1. 程式人生 > >【LeetCode筆記】Binary Tree Level Order Traversal II 二叉樹按層遍歷,反向輸出

【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,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

[
  [15,7],
  [9,20],
  [3]
]
思路:

這題不難,按層遍歷一個queue就行,如果要反向輸出,再用一個stack調整一下順序就好了!

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> v_e;
        if (root==NULL) 
            return v_e;
        else{
            TreeNode* t;
            queue<TreeNode*> q;
            q.push(root);
            t = q.front();
            stack<vector<int>> s;
            while (!q.empty()){
                int size = q.size();
                vector<int> v;
                for (int i = 0; i < size;i++){
                    t = q.front();
                    v.push_back(t->val);
                    q.pop();
                    if(t->left!=NULL) 
                        q.push(t->left);
                    if(t->right!=NULL) 
                        q.push(t->right);
                }
                s.push(v);
            }
            while(!s.empty()){
                v_e.push_back(s.top());
                s.pop();
            }
            return v_e;
        }
    }
};


相關推薦

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

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

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

LeetCode:102. Binary Tree Level Order Traversal的層次

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given b

[LintCode] Binary Tree Level Order Traversal的層次

描述 給出一棵二叉樹,返回其節點值的層次遍歷(逐層從左往右訪問) 樣例 給一棵二叉樹 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分層遍歷結果:

[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 ex

LeetCode | Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Gi

Binary Tree Level Order Traversal-儲存並返回結果集)

題目描述 Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given

leetcode107Binary 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 fr

LeetCode演算法題-Binary Tree Level Order Traversal II(Java實現)

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

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

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

LeetCode 314. Binary Tree Vertical Order Traversal垂直

Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column). If two nod

LeetCode-面試算法經典-Java實現107-Binary Tree Level Order Traversal IIII

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

leetCodeBinary Tree Level Order Traversal python實現

Binary Tree Level Order Traversal 原題連結 實現原理解析 層次遍歷即可 python程式碼實現 class Solution(object): def __init__(self

easy107. Binary Tree Level Order Traversal II 輸出

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

[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

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刷題Easy篇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

Leetcode 107. Binary Tree Level Order Traversal II

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

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