My 3ms Easy Java Solution


  • 0
    H

    public List<String> binaryTreePaths(TreeNode root) {

        List<String> list = new ArrayList<String>();
        String s = new String();
        addPaths(root,list,s);
        return list;
    }
    
    private void addPaths(TreeNode node, List list, String path) {
        
        if ( node == null) return ;
        else if( node.left == null && node.right == null ) {
            
            path = path + Integer.toString(node.val);
            list.add(path);
        }
        else {
             
            path = path + Integer.toString(node.val) + "->" ;
            //recurse to left and right subtree
            addPaths(node.left,list,path);
            addPaths(node.right,list,path);
            
        }
        
        return;
    }

Log in to reply
 

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