Tree: Postorder Traversal

Sort by

recency

|

227 Discussions

|

  • + 0 comments
    public static void postOrder(Node root) {
            if(root != null){
                postOrder(root.left);
                postOrder(root.right);
                System.out.print(root.data+" ");
            }
        }
    
  • + 0 comments

    Javascript:

    function postOrder(root) {
    	  let result = []
    		let values = ""
        
        function dfsPostTraverse(currentNode){
            if(currentNode == null){
                return;
            }
            dfsPostTraverse(currentNode.left)
            dfsPostTraverse(currentNode.right)
            result.push(currentNode.data)
        }
        
        dfsPostTraverse(root)
        
        for(let i=0; i<result.length; i++){
            values += result[i] + " "
        }
        console.log(values)
    }
    
  • + 0 comments
    def postOrder(root):
        result = []
        
        def traverse(node):
            if not node:
                return
            traverse(node.left)
            traverse(node.right)
            result.append(str(node.info))
            
        traverse(root)
        print(" ".join(result))
    
  • + 0 comments

    solution for java

    public static void postOrder(Node root) {

        if(root==null) return;
    
        postOrder(root.left);
    
        postOrder(root.right);
        System.out.print(root.data+" ");
    }
    
  • + 0 comments

    Solution with Javascript:

    function postOrder(root) { const stack = iterate(root)

    console.log(stack.join(' ')) }

    function iterate(root, stack = []) { if (root == null) { return }

    iterate(root.left, stack) iterate(root.right, stack)

    stack.push(root.data)

    return stack } `