/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> ret = new ArrayList<>();
if(root == null)
return ret;
solve(root,"",ret);
return ret;
}
public void solve(TreeNode root, String cur, List<String> ret){
if(root == null)
return;
cur += root.val;
if(root.left == null && root.right == null){
ret.add(cur);
}else{
solve(root.left, cur+"->", ret);
solve(root.right, cur+"->", ret);
}
}
}
257. 二叉树的所有路径 Java版
最新推荐文章于 2023-09-08 07:49:05 发布