Clear recursion solution with Java


  • 0
    B

    /**

    • Definition for a binary tree node.
    • public class TreeNode {
    • int val;
      
    • TreeNode left;
      
    • TreeNode right;
      
    • TreeNode(int x) { val = x; }
      
    • }
      */
      public class Solution {
      public List<Integer> postorderTraversal(TreeNode root) {
      if(root == null) {
      return new ArrayList<Integer>();
      }
      List<Integer> myList = new ArrayList<Integer>();
      List<Integer> leftTraversalList = postorderTraversal(root.left);
      List<Integer> rightTraversalList = postorderTraversal(root.right);
      myList.addAll(leftTraversalList);
      myList.addAll(rightTraversalList);
      myList.add(root.val);
      return myList;
      }
      }

Log in to reply
 

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