1. 程式人生 > >107. Binary Tree Level Order Traversal II (二叉樹由底向上層次遍歷)

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]
]
/**
 * 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>> list = new LinkedList<List<Integer>>();
        if(root==null)
        	return list;
        List<Integer> intList = new LinkedList<Integer>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        TreeNode t = null;
        queue.offer(root);
        int curCount=0,curNum=1,nextCount=1;
        while(!queue.isEmpty()){
        	t=queue.poll();
        	intList.add(t.val);
        	if(t.left!=null){
        		queue.offer(t.left);
        		nextCount++;
        	}if(t.right!=null){
        		queue.offer(t.right);
        		nextCount++;
        	}
        	if(++curCount==curNum){
        		list.add(0,intList);
        		intList=new LinkedList<Integer>();
        		curNum=nextCount;
        	}
        }return list;
    }
}


相關推薦

107. Binary Tree Level Order Traversal II 向上層次

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

【LeetCode-面試算法經典-Java實現】【107-Binary Tree Level Order Traversal II層序II

lin -m length ret itl pub util 實現類 markdown 【107-Binary Tree Level Order Traversal II(二叉樹層序遍歷II)】 【LeetCode-面試算法經典-Java實現】【全

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 t

LeetCode | Binary Tree Level Order Traversal II層序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

LeetCode(Binary Tree Level Order Traversal, 2,Zigzag)層次

1,題目要求: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For exam

[LeetCode] 107. Binary Tree Level Order Traversal II Java

example 二叉樹 ever val 代碼 blog == order public 題目: Given a binary tree, return the bottom-up level order traversal of its nodes‘ values. (

LeetCode 107.Binary Tree Level Order Traversal II

spa values 個數 -s 判斷 ret 層次遍歷 pub ger Given a binary tree, return the bottom-up level order traversal of its nodes‘ values. (ie, from left

【easy】107. Binary Tree Level Order Traversal II 按層輸出

ini == nod otto cto velt tree 輸出 int 按層輸出二叉樹,廣度優先。 3 / 9 20 / 15 7 [ [15,7], [9,20], [3] ] /** * Definit

107. Binary Tree Level Order Traversal II

結果 -i htm rip pri log oot https etc 原題鏈接:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/ 這道題目是 http://www

LeetCode演算法題-Binary Tree Level Order Traversal IIJava實現

這是悅樂書的第165次更新,第167篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第24題(順位題號是107)。給定二叉樹,返回其節點值的自下而上級別順序遍歷(即從左到右,逐層逐層)。例如: 給定二叉樹[3,9,20,null,null,15,7], 3

leetcode: 107. Binary Tree Level Order Traversal II

Difficulty Easy. Problem Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, l

Leetcode 107. Binary Tree Level Order Traversal II

文章作者:Tyan 部落格:noahsnail.com  |  CSDN  |  簡書 1. Description 2. Solution Version 1 /** * Definition

LeetCode 107.Binary Tree Level Order Traversal II (層次 II)

題目描述: 給定一個二叉樹,返回其節點值自底向上的層次遍歷。 (即按從葉子節點所在層到根節點所在的層,逐層從左向右遍歷) 例如: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7

[LeetCode&Python] Problem 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).

leetcode 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 r

107. Binary Tree Level Order Traversal II - Easy

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 roo

[leetcode]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

leetcode-java-107. Binary Tree Level Order Traversal II

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode

LeetCode-107. Binary Tree Level Order Traversal II (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

UVa 122 Trees on the level鏈式的建立和層次

構建 void target .net color 鏈接 struct failed div 題目鏈接: https://cn.vjudge.net/problem/UVA-122 1 /* 2 問題 3 給出每個節點的權值和路線,輸出該二叉樹的層次遍歷序列。