Simple Javascript solution


  • 0
    J

    DFS.

    var postorderTraversal = function(root) {
        if (!root) return [];
        var node = root;
        var result = []
        
        var traverse = function(node) {
            if (node.left) {
                traverse(node.left)
            }
            
            if (node.right) {
                traverse(node.right)
            }
            
            result.push(node.val);
        }
        
        traverse(node);
        
        return result;
    };

  • 0
    F

    @jordan34 I believe the interest of this exercise is to find an iterative solution. As it is said, a recursive solution is trivial (and can be achieved in one line with no closure)


Log in to reply
 

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