Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ed63eba commit 413ec73Copy full SHA for 413ec73
0366.寻找完全二叉树的叶子节点/0366-寻找完全二叉树的叶子节点.py
@@ -0,0 +1,35 @@
1
+# Definition for a binary tree node.
2
+# class TreeNode(object):
3
+# def __init__(self, x):
4
+# self.val = x
5
+# self.left = None
6
+# self.right = None
7
+
8
+class Solution(object):
9
+ def findLeaves(self, root):
10
+ """
11
+ :type root: TreeNode
12
+ :rtype: List[List[int]]
13
14
+ from collections import defaultdict
15
+ self.dic = defaultdict(list)
16
+ res = []
17
+ def get_Height(node):
18
+ if not node:
19
+ return -1
20
+ lh = get_Height(node.left)
21
+ rh = get_Height(node.right)
22
+ h = max(lh, rh) + 1
23
+ self.dic[h].append(node.val)
24
+ return h
25
26
+ get_Height(root)
27
+ # print self.dic
28
+ h = 0
29
+ while 1:
30
+ if h not in self.dic:
31
+ break
32
+ res.append(self.dic[h])
33
+ h += 1
34
+ return res
35
0 commit comments