leetcode解题之107 # Binary Tree Level Order Traversal II Java版
2017-03-13 20:08
225 查看
107. Binary Tree Level Order Traversal II
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,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
运用广度优先搜索方法,运用队列的方法,每次遍历一层的叶节点,并把下一层的叶节点加入到队列中。
每次遍历一层节点结束时候,将该层节点组成的list放到整体的list之前,实现由底到高的排列。
补充:
/*
queue方法补充:
add 增加一个元索。 如果队列已满,则抛出一个IIIegaISlabEepeplian异常
remove 移除并返回队列头部的元素。如果队列为空,则抛出一个NoSuchElementException异常
element 返回队列头部的元素。 如果队列为空,则抛出一个NoSuchElementException异常
offer 添加一个元素并返回true。 如果队列已满,则返回false
poll 移除并返问队列头部的元素 。如果队列为空,则返回null
peek 返回队列头部的元素。 如果队列为空,则返回null
put 添加一个元素。 如果队列满,则阻塞
take 移除并返回队列头部的元素。 如果队列为空,则阻塞
remove、element、offer 、poll、peek 其实是属于Queue接口
*/
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,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
运用广度优先搜索方法,运用队列的方法,每次遍历一层的叶节点,并把下一层的叶节点加入到队列中。
每次遍历一层节点结束时候,将该层节点组成的list放到整体的list之前,实现由底到高的排列。
//树节点定义 public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }
public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> list = new LinkedList<List<Integer>>(); Queue<TreeNode> queue = new LinkedList<TreeNode>(); if (root == null) return list; queue.offer(root); while (!queue.isEmpty()) { int num = queue.size(); List<Integer> levelList = new LinkedList<Integer>(); for (int i = 0; i < num; i++) { TreeNode node = queue.peek(); if (node.left != null) queue.offer(node.left); if (node.right != null) queue.offer(node.right); levelList.add(queue.poll().val); } // 在链表开始处插入 list.add(0, levelList); } return list; }
补充:
/*
queue方法补充:
add 增加一个元索。 如果队列已满,则抛出一个IIIegaISlabEepeplian异常
remove 移除并返回队列头部的元素。如果队列为空,则抛出一个NoSuchElementException异常
element 返回队列头部的元素。 如果队列为空,则抛出一个NoSuchElementException异常
offer 添加一个元素并返回true。 如果队列已满,则返回false
poll 移除并返问队列头部的元素 。如果队列为空,则返回null
peek 返回队列头部的元素。 如果队列为空,则返回null
put 添加一个元素。 如果队列满,则阻塞
take 移除并返回队列头部的元素。 如果队列为空,则阻塞
remove、element、offer 、poll、peek 其实是属于Queue接口
*/
相关文章推荐
- leetcode79 word serach 解题报告
- leetcode 416 python 解题报告
- [Leetcode] 233. Number of Digit One 解题报告
- LeetCode 240. Search a 2D Matrix II 解题报告
- leetcode第十六周解题总结--图
- 【LeetCode】Array Partition I 解题报告
- [Leetcode] 346. Moving Average from Data Stream 解题报告
- [LeetCode] 419. Battleships in a Board 解题报告
- [Leetcode] 348. Design Tic-Tac-Toe 解题报告
- leetcode解题报告:338. Counting Bits
- LeetCode(1) Two Sum解题报告
- [leetcode] 228. Summary Ranges 解题报告
- 【LeetCode】107.Binary Tree Level Order Traversal II(Easy)解题报告
- [Leetcode] 761. Special Binary String 解题报告
- leetcode解题之532. K-diff Pairs in an Array Java版
- 【LeetCode】617.Merge Two Binary Trees(Easy)解题报告
- leetCode Linked List Cycle 解题分享
- LeetCode: Minimum Depth of Binary Tree 解题报告
- leetcode部分解题思路
- 【LeetCode】693. Binary Number with Alternating Bits 解题报告