标签:
Given a binary tree, return the bottom-up level order traversal of its nodes‘ values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / 9 20 / 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
解题思路:
修改下Java for LeetCode 102 Binary Tree Level Order Traversal即可解决,JAVA实现如下:
public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> list = new ArrayList<List<Integer>>(); if (root == null) return list; Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root); while (queue.size() != 0) { List<Integer> alist = new ArrayList<Integer>(); for (TreeNode child : queue) alist.add(child.val); list.add(new ArrayList<Integer>(alist)); Queue<TreeNode> queue2=queue; queue=new LinkedList<TreeNode>(); for(TreeNode child:queue2){ if (child.left != null) queue.add(child.left); if (child.right != null) queue.add(child.right); } } Collections.reverse(list); return list; }
Java for LeetCode 107 Binary Tree Level Order Traversal II
标签:
原文地址:http://www.cnblogs.com/tonyluis/p/4524691.html