145. Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values.

 

Solution 1: class Solution {     public List postorderTraversal(TreeNode root) {     List list = new ArrayList<>();     if(root == null) return list;     Stack stack = new Stack<>();     stack.push(root);     while(!stack.empty()){         root = stack.pop();         list.add(0, root.val);         if(root.left != null)              stack.push(root.left);         if(root.right != null)              stack.push(root.right);     }     return list;   } }   Solution 2: class Solution {     List result = new ArrayList();     public List postorderTraversal(TreeNode root) {              helper(root);                             return result;     }          private void helper(TreeNode root) {          if (root == null)             return;          if (root.left != null)              helper(root.left);          if (root.right != null)              helper(root.right);          result.add(root.val);            } }
上一篇:unity模仿瓶子中的液体晃动


下一篇:常用命令汇总