-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_leaves.py
71 lines (50 loc) · 1.4 KB
/
find_leaves.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
366. Find Leaves of Binary Tree
Medium
Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.
Example:
Input: [1,2,3,4,5]
1
/ \
2 3
/ \
4 5
Output: [[4,5,3],[2],[1]]
Explanation:
1. Removing the leaves [4,5,3] would result in this tree:
1
/
2
2. Now removing the leaf [2] would result in this tree:
1
3. Now removing the leaf [1] would result in the empty tree:
[]
"""
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def print_tree(node):
if node.left==None and node.right==None:
print(node.val)
return None
else:
node.left=print_tree(node.left)
node.right=print_tree(node.right)
return node
def Is_It_Child(node):
if node.left==None and node.right==None:
return True
if __name__=="__main__":
Root=TreeNode(1)
first_node=TreeNode(2)
second_node=TreeNode(3)
third_node=TreeNode(4)
fourth_node=TreeNode(5)
Root.left=first_node
Root.right=second_node
Root.left.left=third_node
Root.left.right=fourth_node
#print(Is_It_Child(Root.left.right))
print_tree(Root)