JavaScript DFS Solution


  • 0
    var binaryTreePaths = function(root) {
        if(!root) return [];
        var result = [];
        function path(root, str){
            if(!root.left && !root.right) result.push(str + root.val);
            if(root.left) path(root.left, str + root.val + "->");
            if(root.right) path(root.right, str + root.val + "->");
        }
        path(root, "");
        return result;
    };

Log in to reply
 

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