480.Binary Tree Paths
1.Description(Easy)
Given a binary tree, return all root-to-leaf paths.
Example
Given the following binary tree:
1
/ \
2 3
\
5All root-to-leaf paths are:
[
"1->2->5",
"1->3"
]2.Code
先把root的值放进去,之后每次dfs可以直接在前面加上“->”.
dfs先判断如果是leaf,就直接把path加入result,在进行左右子树的dfs.
Last updated
Was this helpful?