Skip to content

Commit fc9441b

Browse files
committed
tree problems added.
1 parent 480eb29 commit fc9441b

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

binary-tree/center-of-a-tree.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from queue import Queue
2+
3+
# Refer the below link for problem.
4+
# https://www.youtube.com/watch?v=nzF_9bjDzdc&list=PLDV1Zeh2NRsDfGc8rbQ0_58oEZQVtvoIc&index=4
5+
6+
7+
def getCentreOfTree(tree):
8+
leaves = []
9+
degree = {}
10+
for node in tree:
11+
degree[node] = len(tree[node])
12+
if degree[node] <= 1: # nodes with degree = 0, 1
13+
leaves.append(node)
14+
count = len(leaves)
15+
n = len(tree)
16+
while count < n:
17+
new_leaves = []
18+
for node in leaves:
19+
for neighbor in tree[node]:
20+
degree[neighbor] -= 1
21+
if degree[neighbor] == 1:
22+
new_leaves.append(neighbor)
23+
degree[node] = 0
24+
count += len(new_leaves)
25+
leaves = new_leaves
26+
return leaves
27+
28+
29+
if __name__ == '__main__':
30+
# undirected graph with no cycles therefore a tree
31+
tree = {
32+
0: [1],
33+
1: [0, 3, 4],
34+
2: [3],
35+
3: [1, 2, 6, 7],
36+
4: [1, 5, 8],
37+
5: [4],
38+
6: [3, 9],
39+
7: [3],
40+
8: [4],
41+
9: [6]
42+
}
43+
print(getCentreOfTree(tree))

binary-tree/height-of-binary-tree.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.left = None
6+
self.right = None
7+
8+
9+
class Solution:
10+
def getHeight(self, root: TreeNode) -> int:
11+
if root is None:
12+
return -1
13+
if root.left is None and root.right is None: # leaf node
14+
return 0
15+
return max(self.getHeight(root.left), self.getHeight(root.right)) + 1
16+
17+
18+
root = TreeNode(3)
19+
root.left = TreeNode(5)
20+
root.left.left = TreeNode(6)
21+
root.left.right = TreeNode(2)
22+
root.left.right.left = TreeNode(7)
23+
root.left.right.right = TreeNode(4)
24+
root.right = TreeNode(1)
25+
root.right.left = TreeNode(0)
26+
root.right.right = TreeNode(8)
27+
28+
29+
solution = Solution()
30+
assert solution.getHeight(root) == 3, "test case 1 failed"
31+
assert solution.getHeight(root.right) == 1, "test case 1 failed"
32+
assert solution.getHeight(root.left) == 2, "test case 1 failed"

binary-tree/leaf-node-sum.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.left = None
6+
self.right = None
7+
8+
9+
class Solution:
10+
def getLeafNodeSum(self, root: TreeNode) -> int:
11+
if root is None:
12+
return 0
13+
if root.left is None and root.right is None:
14+
return root.val
15+
total = 0
16+
if root.left:
17+
total += self.getLeafNodeSum(root.left)
18+
if root.right:
19+
total += self.getLeafNodeSum(root.right)
20+
return total
21+
22+
23+
root = TreeNode(3)
24+
root.left = TreeNode(5)
25+
root.left.left = TreeNode(6)
26+
root.left.right = TreeNode(2)
27+
root.left.right.left = TreeNode(7)
28+
root.left.right.right = TreeNode(4)
29+
root.right = TreeNode(1)
30+
root.right.left = TreeNode(0)
31+
root.right.right = TreeNode(8)
32+
33+
34+
solution = Solution()
35+
output = solution.getLeafNodeSum(root)
36+
assert output == 25, "test case 1 failed"

binary-tree/tree-encoding.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from queue import Queue
2+
3+
# Refer the below link for problem.
4+
# https://www.youtube.com/watch?v=nzF_9bjDzdc&list=PLDV1Zeh2NRsDfGc8rbQ0_58oEZQVtvoIc&index=4
5+
6+
7+
def dfs(node, tree):
8+
if node is None:
9+
return ""
10+
encoding = '0'
11+
for child in tree[node]:
12+
childEncoding = dfs(child, tree)
13+
encoding += childEncoding
14+
encoding += '1'
15+
return encoding
16+
17+
18+
def getEncoding(root, tree):
19+
"""
20+
Perform a dfs and append 0 as you enter a node and 1 when you leave the node.
21+
"""
22+
return dfs(root, tree)
23+
24+
25+
if __name__ == '__main__':
26+
# undirected graph with no cycles therefore a tree
27+
tree = {
28+
0: [],
29+
1: [0, 2],
30+
2: [],
31+
3: [5],
32+
4: [1, 3],
33+
5: []
34+
}
35+
print(getEncoding(4, tree))

0 commit comments

Comments
 (0)