Problem Binary Tree Postorder Traversal

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     }

 

posted @ 2014-06-29 13:54  HaruHaru  阅读(140)  评论(0编辑  收藏  举报