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

Problem Binary Tree Postorder Traversal

时间:2014-07-07 16:45:23      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   strong   os   

Problem Description:

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

Solution:

 1 public List<Integer> postorderTraversal(TreeNode root) {
 2         if (root == null) return new LinkedList<Integer>();
 3         List<Integer> list =  new LinkedList<Integer>();
 4         if (root.left == null && root.right == null) {
 5             LinkedList<Integer> leaf = new LinkedList<Integer>();
 6             leaf.add(root.val);
 7             return leaf;
 8         }
 9          if (root.left != null) {
10             list.addAll(postorderTraversal(root.left));
11         } 
12         if (root.right != null) {
13             list.addAll(postorderTraversal(root.right));
14         }
15         list.add(root.val);
16 
17         return list;
18     }

 

Problem Binary Tree Postorder Traversal,布布扣,bubuko.com

Problem Binary Tree Postorder Traversal

标签:des   style   blog   color   strong   os   

原文地址:http://www.cnblogs.com/liew/p/3815017.html

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