Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution(object):
- def dfs_leftmost(self, root, res):
- if not root or not root.left and not root.right:
- return
- res.append(root.val)
- if root.left:
- self.dfs_leftmost(root.left, res)
- else:
- self.dfs_leftmost(root.right, res)
- def dfs_leaves(self, root, res, exl_node):
- if not root:
- return
- self.dfs_leaves(root.left, res, exl_node)
- if root != exl_node and not root.left and not root.right:
- res.append(root.val)
- self.dfs_leaves(root.right, res, exl_node)
- def dfs_rightmost(self, root, res):
- if not root or not root.left and not root.right:
- return
- if root.right:
- self.dfs_rightmost(root.right, res)
- else:
- self.dfs_rightmost(root.left, res)
- res.append(root.val)
- def boundaryOfBinaryTree(self, root):
- """
- :type root: TreeNode
- :rtype: List[int]
- """
- res = []
- if not root:
- return res
- res.append(root.val)
- self.dfs_leftmost(root.left, res)
- self.dfs_leaves(root, res, root)
- self.dfs_rightmost(root.right, res)
- return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement