美文网首页
Tree:Given a binary tree, return

Tree:Given a binary tree, return

作者: 敲一手烂代码 | 来源:发表于2016-05-20 15:01 被阅读35次
public List<String> binaryTreePaths(TreeNode root) {
        List<String> list = new ArrayList<String>();
        if(root!=null)preOrder(root,"", list);
        return list;
    }
    
    public void preOrder(TreeNode node,String path,List<String> 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

      本文链接:https://www.haomeiwen.com/subject/qvjnrttx.html