首页 > 代码库 > Problem Binary Tree Postorder Traversal

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     }