1. 程式人生 > >LeetCode | Binary Tree Level Order Traversal(二叉樹層序遍歷)

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:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]

題目解析:

層序遍歷,設定佇列,入隊列出佇列即可。

為了能夠更方便的操作,可以設定兩個佇列,當對佇列1進行出佇列,將其子孩子放入佇列2中。遍歷完後,將佇列2中元素賦值給佇列1。

另外一種是隻用一個佇列實現。當開始進行下一層迴圈時,用個變數n儲存當前佇列中元素的個數。然後for迴圈n次,將n個結點的子節點放入佇列。下次迴圈時,再求一次n,就得到新的一層的子節點個數。注意,這裡不能i<queue.size(),必須用n,因為我們動態向佇列中新增元素。

class Solution {
public:
    vector<vector<int> > levelOrder(TreeNode *root) {
        if(root == NULL)
            return res;
        queue<TreeNode *> tree;
        tree.push(root);
        while(!tree.empty()){
            vector<int> node;
            TreeNode *p;
            int n = tree.size();    //如果用一個佇列完成的話,必須先儲存佇列中的值,佇列的值會重新計算。
            for(int i = 0;i < n;i++){
                p = tree.front();
                tree.pop();
                if(p->left) tree.push(p->left);
                if(p->right) tree.push(p->right);
                node.push_back(p->val);
            }
            res.push_back(node);
        }

        return res;
    }
private:
    vector<vector<int> > res;
};


相關推薦

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

[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

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

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 199 Binary Tree Right Side View

Given a binary tree, imagine yourself standing on therightside of it, return the values of the node

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

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

LeetCode 144 Binary Tree Preorder Traversal

Given a binary tree, return thepreordertraversal of its nodes' values. For example: Given binary tr

劍指Offer——:把列印成多行

對於二叉樹的最好的解決辦法就是遞迴。遍歷方法無外乎先序遍歷,中序遍歷,後序遍歷方法以及層序遍歷方法。這裡給大家安利一個關於樹的面試題的連結,博主walkinginthewind比較全面且詳細的介紹了二叉樹相關的面試題:對於層序遍歷,最好的方法就是用佇列記錄遍歷節點的值,按層列

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

LeetCode | Binary Tree Level Order Traversal IIII

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 ,反向輸出

題目: 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 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 145 Binary Tree Postorder Traversal的興許+、叠代

int truct fin for data- right class span popu 翻譯 給定一個二叉樹。返回其興許遍歷的節點的值。 比如: 給定二叉樹為 {1。 #, 2, 3} 1 2 / 3 返回

[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:Given binary tree {3,9

LeetCode 124. Binary Tree Maximum Path Sum最大路徑和

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node

Leetcode 103 的鋸齒形層次 的層次

給定一個二叉樹,返回其節點值的鋸齒形層次遍歷。(即先從左往右,再從右往左進行下一層遍歷,以此類推,層與層之間交替進行)。 例如: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回鋸

Leetcode 103 的鋸齒形層次 的層次

給定一個二叉樹,返回其節點值的鋸齒形層次遍歷。(即先從左往右,再從右往左進行下一層遍歷,以此類推,層與層之間交替進行)。 例如: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7

應用之Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to b

Tree UVA - 548遞歸

out pac col sstream end 遍歷 std 二叉 con 題目鏈接:https://vjudge.net/problem/UVA-548 題目大意:給一顆點帶權(權值各不相同,都是小於10000的正整數)的二叉樹的中序遍歷和後序遍歷,找一個葉子結點使得它