Binary Tree Level Order Traversal II leetcode java

题目

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;  
10         int nextLevCnt = 0;  
11         
12         ArrayList<Integer> levelres = new ArrayList<Integer>();  
13        
14         while(!queue.isEmpty()){  
15             TreeNode cur = queue.poll();  
16             curLevCnt--;  
17             levelres.add(cur.val);  
18             
19             if(cur.left != null){  
20                 queue.add(cur.left);  
21                 nextLevCnt++;  
22             }  
23             if(cur.right != null){  
24                 queue.add(cur.right);  
25                 nextLevCnt++;  
26             }  
27               
28             if(curLevCnt == 0){  
29                 curLevCnt = nextLevCnt;  
30                 nextLevCnt = 0;  
31                 res.add(0,levelres);  //insert one by one from the beginning
32                 levelres = new ArrayList<Integer>();  
33             }  
34         }  
35         return res;  
36     }


Binary Tree Level Order Traversal II leetcode java

时间: 2024-12-15 00:37:01

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

Binary Tree Level Order Traversal II &lt;leetcode&gt;

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 7return its bottom-up level order tra

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

考察点 广度优先遍历--层次遍历 STL内容器的用法 广度优先遍历的时候,首先应该想到的就是借助于队列.还需要在遍历下一层之前保存当前层节点的数量 代码很简单: class Solution { public: vector<vector<int> > levelOrderBottom(TreeNode* root) { vector<vector<int> >vec; if(root==NULL){ return vec; } queue <Tree

leecode Binary Tree Level Order Traversal II java

做完这道题,只能说基本功很重要,数组中套数组就不会用了,过几天吧1做了,看自己到底等没. https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ import java.util.*; /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNod

[leetcode]Binary Tree Level Order Traversal II @ Python

原题地址:http://oj.leetcode.com/problems/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

【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 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 trave

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

Leetcode 树 Binary Tree Level Order Traversal II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Level Order Traversal II Total Accepted: 10080 Total Submissions: 32610 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, l

LeetCode: Binary Tree Level Order Traversal II [107]

[题目] 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

【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 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 traver