未验证 提交 5e5a3f28 编写于 作者: K Keqi Huang 提交者: GitHub

Update 366. Find Leaves of Binary Tree.md

上级 12290d3f
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
AC代码 AC代码
``` ```python
class Solution(object): class Solution(object):
def findLeaves(self, root): def findLeaves(self, root):
""" """
...@@ -26,29 +26,27 @@ class Solution(object): ...@@ -26,29 +26,27 @@ class Solution(object):
:rtype: List[List[int]] :rtype: List[List[int]]
""" """
def findLeaf(root): def findLeaf(root):
if root == None: if not root:
return [] return []
elif root.left == None and root.right == None: elif not root.left and not root.right:
return [root.val] return [root.val]
else: else:
return findLeaf(root.left) + findLeaf(root.right) return findLeaf(root.left) + findLeaf(root.right)
def removeLeaf(root): def removeLeaf(root):
if root == None: if not root:
return None return None
elif root.left == None and root.right == None: elif not root.left and not root.right:
return None return None
else: else:
if root.left: if root.left:
root.left = removeLeaf(root.left) root.left = removeLeaf(root.left)
if root.right: if root.right:
root.right = removeLeaf(root.right) root.right = removeLeaf(root.right)
return root return root
res = [] res = []
while root: while root:
res.append(findLeaf(root)) res.append(findLeaf(root))
root = removeLeaf(root) root = removeLeaf(root)
return res return res
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册