题目

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]
] 题解
这道题跟前面一道是一样的。。
只是存到res的结果顺序不一样罢了。
之前那个就是循序的存
这道题就是每得到一个行结果就存在res的0位置,这样自然就倒序了。 代码如下:
 1     public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
 2         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();  
 3         if(root == null)  
 4             return res;
 5         
 6         LinkedList<TreeNode> queue = new LinkedList<TreeNode>();  
 7         queue.add(root);
 8         
 9         int curLevCnt = 1;  
         int nextLevCnt = 0;  
         
         ArrayList<Integer> levelres = new ArrayList<Integer>();  
        
         while(!queue.isEmpty()){  
             TreeNode cur = queue.poll();  
             curLevCnt--;  
             levelres.add(cur.val);  
             
             if(cur.left != null){  
                 queue.add(cur.left);  
                 nextLevCnt++;  
             }  
             if(cur.right != null){  
                 queue.add(cur.right);  
                 nextLevCnt++;  
             }  
               
             if(curLevCnt == 0){  
                 curLevCnt = nextLevCnt;  
                 nextLevCnt = 0;  
                 res.add(0,levelres);  //insert one by one from the beginning
                 levelres = new ArrayList<Integer>();  
             }  
         }  
         return res;  
     }
												

Binary Tree Level Order Traversal II leetcode java的更多相关文章

  1. Binary Tree Level Order Traversal II——LeetCode

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  2. Binary Tree Level Order Traversal II [LeetCode]

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  3. LeetCode算法题-Binary Tree Level Order Traversal II(Java实现)

    这是悦乐书的第165次更新,第167篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第24题(顺位题号是107).给定二叉树,返回其节点值的自下而上级别顺序遍历(即从左到右 ...

  4. Binary Tree Level Order Traversal II --leetcode C++

    考察点 广度优先遍历--层次遍历 STL内容器的用法 广度优先遍历的时候,首先应该想到的就是借助于队列.还需要在遍历下一层之前保存当前层节点的数量 代码很简单: class Solution { pu ...

  5. 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 ...

  6. 【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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Bzoj2534:后缀自动机 主席树启发式合并

    国际惯例的题面:考虑我们求解出字符串uvu第一个u的右端点为i,第二个u的右端点为j,我们需要满足什么性质?显然j>i+L,因为我们选择的串不能是空串.另外考虑i和j的最长公共前缀(也就是说其p ...

  2. Codeforces.1041F.Ray in the tube(思路)

    题目链接 \(Description\) 有两条平行于\(x\)轴的直线\(A,B\),每条直线上的某些位置有传感器.你需要确定\(A,B\)轴上任意两个整点位置\(x_A,x_B\),使得一条光线沿 ...

  3. BZOJ.4517.[SDOI2016]排列计数(错位排列 逆元)

    题目链接 错位排列\(D_n=(n-1)*(D_{n-1}+D_{n-2})\),表示\(n\)个数都不在其下标位置上的排列数. 那么题目要求的就是\(C_n^m*D_{n-m}\). 阶乘分母部分的 ...

  4. BZOJ4065 : [Cerc2012]Graphic Madness

    因为两棵树中间只有k条边,所以这些边一定要用到. 对于每棵树分别考虑: 如果一个点往下连着两个点,那么这个点往上的那条边一定不能用到. 如果一个点往下连着一个点,那么这个点往上的那条边一定不能用到. ...

  5. BZOJ 3339: Rmq Problem 莫队算法

    3339: Rmq Problem 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3339 Description n个数,m次询问l,r ...

  6. linux C宏定义 转

    写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性等等.下面列举一些成熟软件中常用得宏定义...... 1,防止一个头文件被重复包含 #ifndef COMDEF_H ...

  7. swap文件查看

    建议 Swap 使用单独的分区: a swap file a combination of swap partitions and swap files. Swap 大小的计算公式: M 等于物理内存 ...

  8. 百度地图api---实现新建地图

    调用这个函数 function refresh() { history.go(0);   } 实现了地图新建

  9. delphi 使用工控机控件 iThreadTimes 出现问题, 导致主程序创建页面的时候, 阻塞消息, 不能正常执行。

    delphi  使用工控机控件 iThreadTimes 出现问题, 导致主程序创建页面的时候, 阻塞消息, 不能正常执行. 使用这个控件需要小心 function Tfrm_MainIPC.Open ...

  10. VC 中 编译 boost 1.34.1 或者 1.34.0

    c++boost正则表达式的安装方法 (cy163已成功完成实验 基于宽字节 wstring 解决 "南日" 错误 匹配"12日" expression = & ...