Tree:Given a binary tree, return all root-to-leaf paths.

public List binaryTreePaths(TreeNode root) {
        List list = new ArrayList();
        if(root!=null)preOrder(root,"", list);
        return list;
    }
    
    public void preOrder(TreeNode node,String path,List list) {
        if (node.left==null&&node.right==null) {
            list.add(path+node.val);
        }
        if (node.left!=null) {
            preOrder(node.left, path+node.val+"->", list);
        }
        if (node.right!=null) {
            preOrder(node.right,path+node.val+"->", list);
        }
    }

你可能感兴趣的:(Tree:Given a binary tree, return all root-to-leaf paths.)