My accepted JAVA code for Binary Tree Postorder Traversal


  • -4
    R
    public class Solution {
        List<Integer> result = new ArrayList<Integer>();   // Global ArrayList to save postorder traversal results
        public List<Integer> postorderTraversal(TreeNode root) {
            postOT(root);     // Recursive call
            return result;
        }
        
        public void postOT(TreeNode root) {
            if (root == null) return;    // The Tree is empty
            postOT(root.left);     // First visiting left tree
            postOT(root.right);  // Second visiting right tree
            result.add(root.val);   // Last visiting the node and append the value to the ArrayList
        }
    }

Log in to reply
 

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