CODE 26: Binary Tree Level Order Traversal II
来源:互联网 发布:斑马410网络打印设置 编辑:程序博客网 时间:2024/05/15 08:37
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],]
confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1 / \ 2 3 / 4 \ 5The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}"
.public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {// Start typing your Java solution below// DO NOT write main() functionif (null == root) {return new ArrayList<ArrayList<Integer>>();}ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();Queue<TreeNode> queue = new LinkedList<TreeNode>();ArrayList<Integer> layerVals = new ArrayList<Integer>();queue.offer(root);int layerNumber = 1;while (!queue.isEmpty()) {TreeNode node = queue.poll();layerVals.add(node.val);layerNumber--;if (null != node.left) {queue.offer(node.left);}if (null != node.right) {queue.offer(node.right);}if (layerNumber == 0) {layerNumber = queue.size();ArrayList<Integer> layerValsCpy = new ArrayList<Integer>();layerValsCpy.addAll(layerVals);results.add(0, layerValsCpy);layerVals.clear();}}return results;}
- CODE 26: Binary Tree Level Order Traversal II
- [leet code] Binary Tree Level Order Traversal II
- leetcode 日经贴,Cpp code -Binary Tree Level Order Traversal II
- CODE 31: Binary Tree Level Order Traversal
- [leet code] Binary Tree Level Order Traversal
- binary-tree-level-order-traversal Java code
- binary-tree-level-order-traversal Java code
- LeetCode: Binary Tree Level Order Traversal II
- [LeetCode]Binary Tree Level Order Traversal II
- LeetCode Binary Tree Level Order Traversal II
- [Leetcode] Binary Tree Level Order Traversal II
- Binary Tree Level Order Traversal II
- Binary Tree Level Order Traversal II
- LeetCode Binary Tree Level Order Traversal II
- [LeetCode] Binary Tree Level Order Traversal II
- Binary Tree Level Order Traversal II
- Binary Tree Level Order Traversal II
- Binary Tree Level Order Traversal II
- hdu 1890伸展树(splay tree)区间翻转
- My Summer at Mozilla
- 数据结构之(平衡二叉树)
- Spring中AOP的几个概念
- 尺取法--poj3320
- CODE 26: Binary Tree Level Order Traversal II
- Java面试题之九
- 关于linux中printk的显示等级
- 计算机组成原理
- POJ 1724 最短路费用限制
- 网址批量缩短工具
- uva 10420 List of Conquests
- ch05 Servlet技术
- 29.栈的push、pop 序列