257. Binary Tree Paths
题目描述
方法思路
class Solution {
//Runtime: 6 ms, faster than 100.00%
//Memory Usage: 38.7 MB, less than 18.78%
public List<String> binaryTreePaths(TreeNode root){
List<String> res = new ArrayList<>();
if(root != null) dfs(root, "", res);
return res;
}
public void dfs(TreeNode root, String s, List<String> res){
if(root.left == null && root.right == null)
res.add(s + root.val);
if(root.left != null)
dfs(root.left, s + root.val + "->", res);
if(root.right != null)
dfs(root.right, s + root.val + "->", res);
}
}