/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void paths(TreeNode root , String path , LinkedList<String> paths){
if(root != null){
path += Integer.toString(root.val);
if(root.left == null && root.right == null)
paths.add(path);
else{
path += "->";
paths(root.left,path,paths);
paths(root.right,path,paths);
}
}
}
public List<String> binaryTreePaths(TreeNode root) {
LinkedList<String> paths = new LinkedList();
paths(root,"",paths);
return paths;
}
}
leetcode——257
最新推荐文章于 2023-10-07 13:47:31 发布