My 2ms java solution~


  • 0
    H
    public class Solution {
    
    public void path(TreeNode node, String prePath, List<String> list){
        prePath = prePath+"->"+node.val;
        if(node.left == null && node.right==null){
            list.add(prePath);
        }else{
            if(node.left!=null){
                path(node.left, prePath,list);
            }
            if(node.right!=null){
                path(node.right, prePath,list);
            }
        }
    }
    
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list = new ArrayList<String>();
        if(root == null){
        }
        else if(root.left==null && root.right==null){
            list.add(root.val+"");
        }else{
            if(root.left!=null)
            {
                path(root.left,root.val+"", list);
                
            }
            if(root.right!=null){
                path(root.right,root.val+"", list);
            }
        }
        
        return list;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.