【LeetCode】Binary Tree Postorder Traversal

原创 2013年12月03日 20:59:46

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

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [3,2,1].

Note: Recursive solution is trivial, could you do it iteratively?


/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ArrayList<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<Integer>();
		 if(root == null)
			 return res;
		 recursion(res,root);
		 return res;
    }
     public void recursion(ArrayList<Integer> res, TreeNode root)
	 {
		 if(root.left != null)
		 {
			 recursion(res,root.left);
		 }
		 if(root.right != null)
		 {
			 recursion(res,root.right);
		 }
		 res.add(root.val);
	 }
}


相关文章推荐

Binary Tree Postorder Traversal - LeetCode 145

题目描述: Given a binary tree, return the postorder traversal of its nodes' values. For example: Gi...
  • bu_min
  • bu_min
  • 2015年05月22日 23:01
  • 194

LeetCode Week9: Binary Tree Preorder/InOrder/PostOrder Traversal

这一周完成的题目主要是Tree部分的题目,这里选择3道经典的题来编写题解,分别是Binary Tree Preorder Traversal、Binary Tree Inorder Traversal...

【LEETCODE】106-Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume t...

LeetCode OJ: Binary Tree Postorder Traversal

转战LeetCode,开始不适应,只好先从简单的题目开始 Given a binary tree, return the postorder traversal of its nodes' value...

[Leetcode] Construct Binary Tree from Inorder and Postorder/Preorder and Inorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume t...

Binary Tree Postorder Traversal leetcode145

Given a binary tree, return the postorder traversal of its nodes’ values.For example: Given binary...

LeetCode 7 Binary Tree Postorder Traversal

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

Leetcode: Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume...

#LeetCode# #C++# Binary Tree Postorder Traversal

给一个二叉树,返回后序遍历的节点值 Note:递归很容易,那么迭代呢? 方法一:递归 1.递归结束,root==NULL 2.递归root->left 3.递归root->right 4.存roo...
  • jicoder
  • jicoder
  • 2015年10月02日 11:08
  • 264

Leetcode: Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【LeetCode】Binary Tree Postorder Traversal
举报原因:
原因补充:

(最多只允许输入30个字)