Java simple recursion Solution


  • 0

    Recursion makes things simple dramatically. First parse left, then right, Print the root int the end.

    List<Integer> list = new ArrayList<>();
        public List<Integer> postorderTraversal(TreeNode root) {
            printPostorder(root);
            return list;
        }
    
        void printPostorder(TreeNode node)
        {
            if (node == null)
                return;
    
            // left subtree
            printPostorder(node.left);
    
            // right subtree
            printPostorder(node.right);
    
            list.add(node.val);
        }
    

Log in to reply
 

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