Tree: Postorder Traversal

Sort by

recency

|

233 Discussions

|

  • + 0 comments

    void postOrder(Node *root) { if (root == NULL) { return; // Base case: if node is null, just return } postOrder(root->left); // Traverse left subtree postOrder(root->right); // Traverse right subtree cout << root->data << " "; // Visit the root node (print data) }

  • + 0 comments

    My Java solution with o(n) time complexity and o(h) space complexity:

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

    Python

    def postOrder(root):
        #Write your code here
        if root is None:
            return
        
        postOrder(root.left)
        
        postOrder(root.right)
        
        print(root.info,end=" ")
    
  • + 0 comments

    Very Simple Recursive JavaScript Solution:

    const resp = []

    function helperFunc(root) { if(!root) return; resp.push(root.data) helperFunc(root.right); helperFunc(root.left); }

    function postOrder(root) { helperFunc(root); console.log(resp.reverse().join(' ')); }

  • + 0 comments

    It is asking how to use process.stdout.write to print your answer on JS, not testing the Tree concept.