Binary Tree Paths
Question
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
Tags
- Binary Tree
- DFS
- Recursion
Thought
This problem should use DFS to obtain the path for each branch of the binary tree. Two ways to implement the DFS:
- Recursion
- Queue
Here the implementation of the recursion is provided.
Code
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
result = []
def helper(cur, node):
if node.left is None and node.right is None:
result.append(cur + [str(node.val)])
return
if node.left is not None:
helper(cur + [str(node.val)], node.left)
if node.right is not None:
helper(cur + [str(node.val)], node.right)
if root is None:
return result
helper([], root)
for i, item in enumerate(result):
result[i] = '->'.join(item)
return result