Javascript solution using recursion.


  • 0
    P
    var binaryTreePaths = function(root) {
        let stringList = [];
        if (root === null || root === undefined) return [];
        else {
            searchTree(root, "",stringList);
        }
        return stringList;
    };
    
    var searchTree = function(root, string, stringList) {
        if(root.left === null && root.right === null) stringList.push(string+ root.val);
        if(root.left!== null) searchTree(root.left, string+root.val+"->", stringList);
        if(root.right !== null) searchTree(root.right, string+root.val+"->", stringList);
    };
    
    

Log in to reply
 

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