leetcodeNo.257 二叉树的所有路径(递归未吃透)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List<String> list=new ArrayList<String>();
    String path="";
    public List<String> binaryTreePaths(TreeNode root) {
       if(root!=null)
           binaryTreePaths(path,root);
        return list;
    }
    public void binaryTreePaths(String path,TreeNode root){
        if(root.left==null&&root.right==null){
            list.add(path+root.val);//每递归到底一次  就用list add字符串一次,就是一条路径
            
        }
        if(root.left!=null){
            binaryTreePaths(path+root.val+"->",root.left);
        }
        if(root.right!=null)
            binaryTreePaths(path+root.val+"->",root.right);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33399567/article/details/89159994