首页 > 其他 > 详细

Problem Binary Tree Postorder Traversal

时间:2014-07-07 16:44:27      阅读:305      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!