Binary Tree Level Order Traversal II 解答
Question
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]
- ]
Solution
Same method as "Binary Tree Level Order Traversal". Note that for ArrayList, it has function add(int index, E element).
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- public class Solution {
- public List<List<Integer>> levelOrderBottom(TreeNode root) {
- List<List<Integer>> result = new ArrayList<List<Integer>>();
- if (root == null)
- return result;
- Deque<TreeNode> prev = new ArrayDeque<TreeNode>();
- Deque<TreeNode> current;
- TreeNode tmpNode;
- prev.addLast(root);
- while (prev.size() > 0) {
- current = new ArrayDeque<TreeNode>();
- List<Integer> tmpList = new ArrayList<Integer>();
- while (prev.size() > 0) {
- tmpNode = prev.pop();
- if (tmpNode.left != null)
- current.addLast(tmpNode.left);
- if (tmpNode.right != null)
- current.addLast(tmpNode.right);
- tmpList.add(tmpNode.val);
- }
- prev = current;
- result.add(0, tmpList);
- }
- return result;
- }
- }
Binary Tree Level Order Traversal II 解答的更多相关文章
- 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...
- Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...
- LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...
- 102/107. Binary Tree Level Order Traversal/II
原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...
- 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)
Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...
- LeetCode_107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal II Easy Given a binary tree, return the bottom-up level order ...
- 63. Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 【Binary Tree Level Order Traversal II 】cpp
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
随机推荐
- hdu 4336 Card Collector(期望 dp 状态压缩)
Problem Description In your childhood, people in the famous novel Water Margin, you will win an amaz ...
- C# 制作 仪表
以前在百度写的文档,转移到此处 前些天在做NetAnalyzer时,需要使用一个指针仪表,网上看了一下,也有人做过,但是大部分都是收费的,本着自力更生的原则,于是决定自己设计一个,今天拿出来有读者分享 ...
- 使用批处理bat作为日期系统日期的前三天
在管理server它经常是依据一天来推断载日期系统日志文件,例如,上周五,周一的需要上传日志.上传日志的日期前一天,日志文件命名的日期.这需要获得的日期的前三天.或之前n当天日期. 批量绑定vbs可以 ...
- 浅谈Android系统开发中LOG的使用
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6581828 在程序开发过程中,LOG是广泛使用 ...
- js获取某个标签中的信息
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- ToString()和Convert.ToString()的区别
ToString()和Convert.ToString()的区别 一般情况下,这两种方法都可以通用,但是当返回的数据类型中有可能出现null值时如果调用ToString方法了,就会返回NullRefe ...
- (转) 制作 Clonezilla live 启动盘
GNU/Linux Method A: Tuxboot 下載 GNU/Linux 版本使用的 Tuxboot 在您的環境 在 GNU/Linux 下, 請依 指示 來執行 Tuxboot 並安裝再生龍 ...
- cocos2d-x创建工程批处理
cd /d D:\cocos2d-x-2.2.2\cocos2d-x-2.2.2\tools\project-creator create_project.py -project %1 -packag ...
- Install cv2.so for Anaconda
sudo apt-get install python-opencv cp /usr/lib/python2.7/dist-packages/cv2.so /opt/anaconda/lib/pyth ...
- stretchlim函数分析
在看imadjust代码时,看到stretchlim函数,特此分析一下,代码注释如下 function lowhigh = stretchlim(varargin) %STRETCHLIM Find ...