码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]Binary Tree Postorder Traversal

时间:2014-08-08 23:58:26      阅读:485      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   strong   

Binary Tree Postorder Traversal

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?

算法思路:

最简单的后序遍历。不解释了。

代码如下:

 1 public class Solution {
 2     List<Integer> res = new ArrayList<Integer>();
 3     public List<Integer> postorderTraversal(TreeNode root) {
 4         if(root == null) return res;
 5         if(root.left != null) postorderTraversal(root.left);
 6         if(root.right != null) postorderTraversal(root.right);
 7         res.add(root.val);
 8         return res;
 9     }
10 }

 

[leetcode]Binary Tree Postorder Traversal,布布扣,bubuko.com

[leetcode]Binary Tree Postorder Traversal

标签:des   style   blog   http   color   os   io   strong   

原文地址:http://www.cnblogs.com/huntfor/p/3900239.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!