LeetcCode102 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). (Easy)

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   /   9  20
    /     15   7

return its level order traversal as:

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

分析:

层序遍历,就是BFS的思路,利用队列把每一行的节点存进去,然后一行一行读出。

注意每次循环用读取size的方式确定这一行到哪里结束。

代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 private:
12     vector<vector<int>> result;
13 public:
14     vector<vector<int>> levelOrder(TreeNode* root) {
15         if (root == nullptr) {
16             return result;
17         }
18         queue<TreeNode* > que;
19         que.push(root);
20         while (!que.empty()) {
21             int sz = que.size();
22             vector<int> temp;
23             for (int i = 0; i < sz; ++i) {
24                 TreeNode* cur = que.front();
25                 que.pop();
26                 temp.push_back(cur -> val);
27                 if (cur -> left != nullptr) {
28                     que.push(cur -> left);
29                 }
30                 if (cur -> right != nullptr) {
31                     que.push(cur -> right);
32                 }
33             }
34             result.push_back(temp);
35         }
36         return result;
37     }
38 };
时间: 2024-10-04 18:04:27

LeetcCode102 Binary Tree Level Order Traversal的相关文章

[LeetCode]Binary Tree Level Order Traversal II

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 re

Leetcode题解(1):L102/Binary Tree Level Order Traversal

L102: 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

33: Binary Tree Level Order Traversal II

/************************************************************************/        /*       33:      Binary Tree Level Order Traversal II                                         */        /**************************************************************

Binary Tree Level Order Traversal II 解题思路

思路: 与Binary Tree Level Order Traversal I 几乎一样.只是最后将结果存放在栈里,然后在栈里再传给向量即可. 再次总结思路: 两个queue,先把第一个放进q1,循环q1是否为空,不为空就读取并出列,如果root有孩子就放入q2,最后清空q2. 注意: for循环的时候不要使用vector.size()这类作为最大值判断,由于vector的size可能不断的减小,这回导致遍历不完的情况发生. 即: int count = sret.size(); for(in

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *

leetCode 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  

32: Binary Tree Level Order Traversal

/************************************************************************/        /*       32:      Binary Tree Level Order Traversal                                          */        /****************************************************************

Binary Tree Level Order Traversal java实现

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] ] 实现的关键在

[leetcode]Binary Tree Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal/ 题意:二叉树的层序遍历的实现. 解题思路:二叉树的层序遍历可以用bfs或者dfs来实现.这里使用的dfs实现,代码比较简洁.实际上,二叉树的先序遍历就是dfs实现.   比如一棵树如下: 1 /  \ 2       3 /    \    /   \ 4     5  6    7    二叉树的先序遍历为{1,2,4,5,3,6,7},可以看到这个遍