Tree: Postorder Traversal

Sort by

recency

|

232 Discussions

|

  • + 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.

  • + 0 comments

    Haskell

    module Main where
    
    data Tree = Empty | Node Int Tree Tree
    
    instance Show Tree where
        show Empty = ""
        show (Node x l r) =
            show l ++ show r ++ show x ++ " "
    
    insert :: Tree -> Int -> Tree
    insert Empty x = Node x Empty Empty
    insert (Node k left right) x
        | x < k = Node k (insert left x) right
        | otherwise = Node k left (insert right x)
    
    parseTree :: [Int] -> Tree
    parseTree = foldl insert Empty
    
    main :: IO ()
    main = do
        _ <- getLine
        seq <- map read . words <$> getLine :: IO [Int]
        print $ parseTree seq