1. 程式人生 > >[leetcode]107.Binary Tree Level Order Traversal II

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

【程式碼】

/*********************************
*   日期:2014-10-16
*   作者:SJF0115
*   題號: Binary Tree Level Order Traversal II 
*   來源:https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/
*   結果:AC
*   來源:LeetCode
*   總結:
**********************************/
#include <iostream>
#include <malloc.h>
#include <vector>
#include <queue>
using namespace std;

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> > vec;
        vector<int> v;
        if(root == NULL){
            return vec;
        }
        queue<TreeNode*> q;
        q.push(root);
        //當前層的個數
        int count = 1;
        //下一層的個數
        int nextCount = 0;
        while(!q.empty()){
            //取佇列頭元素
            TreeNode* p = q.front();
            //儲存元素
            v.push_back(p->val);
            q.pop();
            count--;
            //左右子節點
            if(p->left){
                q.push(p->left);
                nextCount++;
            }
            if(p->right){
                q.push(p->right);
                nextCount++;
            }
            //一層訪問完畢
            if(count == 0){
                count = nextCount;
                nextCount = 0;
                vec.insert(vec.begin(),v);
                v.clear();
            }
        }
        return vec;
    }
};

//按先序序列建立二叉樹
int CreateBTree(TreeNode* &T){
    char data;
    //按先序次序輸入二叉樹中結點的值(一個字元),‘#’表示空樹
    cin>>data;
    if(data == '#'){
        T = NULL;
    }
    else{
        T = (TreeNode*)malloc(sizeof(TreeNode));
        //生成根結點
        T->val = data-'0';
        //構造左子樹
        CreateBTree(T->left);
        //構造右子樹
        CreateBTree(T->right);
    }
    return 0;
}

int main() {
    Solution solution;
    TreeNode* root(0);
    CreateBTree(root);
    vector<vector<int> > v = solution.levelOrderBottom(root);
    for(int i = 0;i < v.size();i++){
        for(int j = 0;j < v[i].size();j++){
            cout<<v[i][j];
        }
        cout<<endl;
    }
}
//13#5##49###


相關推薦

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

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

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實現】【全

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

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

【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

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

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】【107Binary 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刷題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 Binary Tree Level Order Traversal II

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

[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