题目:

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

    3
/ \
9 20
/ \
15 7

return its level order traversal as:

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

分析:

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

可以立刻想到两种方法,也就是BFS和DFS。

先说BFS,BFS实际上就是一层一层的遍历,开辟两个数组,分别存放当前处理的节点和当前处理节点的孩子节点,也就是下一层的节点,每次处理完一层,将当前和下一轮数组进行交换,注意每一轮处理之前别忘了清空下一轮数组。

DFS我们写一个辅助函数,其中参数h记录当前处理的层数,当层数大于等于我们的结果数组时,就应该新添加一行,用来存储这一层的节点。这道题前序遍历,中序遍历,后续遍历都可以,只要记住先访问左子树后访问右子树就行。

程序:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//BFS
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(root == nullptr) return {};
vector<vector<int>> res;
vector<TreeNode*> curr, next;
curr.push_back(root);
while(!curr.empty()){
vector<int> temp;
next.clear();
for(auto i:curr){
if(i->left) next.push_back(i->left);
if(i->right) next.push_back(i->right);
temp.push_back(i->val);
}
res.push_back(temp);
curr.swap(next);
}
return res;
}
};
//DFS
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
levelOrder(root,);
return res;
}
void levelOrder(TreeNode* root, int h) {
if(root == nullptr) return;
if(res.size() <= h) res.push_back({});
levelOrder(root->left, h+);
res[h].push_back(root->val);
levelOrder(root->right, h+);
}
private:
vector<vector<int>> res;
};

LeetCode 102. Binary Tree Level Order Traversal02. 二叉树的层次遍历 (C++)的更多相关文章

  1. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  2. leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历

    基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行 ...

  3. 【LeetCode】Binary Tree Level Order Traversal(二叉树的层次遍历)

    这道题是LeetCode里的第102道题. 题目要求: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15 ...

  4. 102 Binary Tree Level Order Traversal 二叉树的层次遍历

    给定一个二叉树,返回其按层次遍历的节点值. (即zhu'ceng'de,从左到右访问).例如:给定二叉树: [3,9,20,null,null,15,7],    3   / \  9  20    ...

  5. [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, ...

  6. 【LeetCode】102. Binary Tree Level Order Traversal 二叉树的层序遍历 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://lee ...

  7. Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS

    二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  8. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

  9. Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历

    给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...

随机推荐

  1. Codeforces Round #573 (Div. 1)

    Preface 军训终于结束了回来补一补之前的坑发现很多题目题意都忘记了 这场感觉难度适中,F由于智力不够所以弃了,E的话石乐志看了官方英文题解才发现自己已经胡了一大半就差实现了233 水平下降严重. ...

  2. (二十五)golang--数组

    数组:存放多个同一类型的数据.在Go中,数组也是一种值类型数组的基本定义: 数组的内存布局: 数组的地址可以用&取出,且它的地址就是第一个元素的地址 数组不用被被初始化而默认是有值的: 数组中 ...

  3. 解决 Ubuntu16.04 + opencv4.1 源码编译错误 Makefile:160: recipe for target 'all' failed

    最近源码编译 opencv,出现下面的错误 [ %] Built target opencv_dnn Makefile:: recipe for target 'all' failed google ...

  4. AngleSharp 实战(05)之遍历内部子元素(x)元素,尝试着获取元素的 Attr 和 InnerText

    直接贴代码了: using System; using System.Linq; using System.Threading.Tasks; using AngleSharp; using Angle ...

  5. 关于VS2017离线安装的一点扩充说明

    转自:https://www.cnblogs.com/dunitian/p/8051985.html 其实逆天不推荐自己慢慢离线,找个离线包更新下再打包更快 Key:http://www.cnblog ...

  6. JVM的监控工具之jvisual

    VisualVM(All-in-One Java Trouble shootingTool)是到目前为止随JDK发布的功能最强大的运行监视和故障处理程序,并且可以预见在未来一段时间内都是官方主力发展的 ...

  7. IDEA的常用配置(Maven)一键导入及优化内存

    IDEA的常用配置一键导入 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载如图的压缩包 下载完成后解压缩,点击settings_bak,你会看 ...

  8. WPF xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

    <Button Grid.Row="1" Content="Load Data" BorderBrush="Black" Border ...

  9. 打开centos7图形化窗口

    1. yum groupinstall "X Window System" 2. export DISPLAY=172.16.4.240:0.0 3. yum -y install ...

  10. java 学习 进阶之 一 (线程基础)

    一.线程安全 线程安全的概念:当多个线程访问某一个类(对象或方法)时.这个类始终都能表现出正确的行为那么这个类(对象或方法)就是线程安全的. synchronized:可以在任何对象及方法上加锁,而加 ...