思路:
DFS找路径。
java code:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public void dfs(TreeNode root, String path, List<String> ans) {
if(root.left == null && root.right ==null) ans.add(path + root.val);
if(root.left != null) dfs(root.left, path + root.val + "->", ans);
if(root.right != null) dfs(root.right, path + root.val + "->", ans);
}
public List<String> binaryTreePaths(TreeNode root) {
List<String> ans = new ArrayList<String>();
if(root != null) dfs(root, "", ans);
return ans;
}
}