【LeetCode】二叉树的最大深度
【问题】给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
/ \ / \
7 返回它的最大深度 3 。
【DFS解法】
我们使用栈结构来储存每个节点root以及该节点的深度deep,由于对tuple的使用还不太熟练,需要多练习,一次使用tuple来讲树结构体指针和对应的整型变量深度。从根节点开始遍历,首先一直遍历左子节点,并将节点压入栈中,如果左子节点为空,则从栈中弹出,并开始遍历弹出节点的右子节点。这个过程也就相当于回溯了,回到上一级去。
**
* 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:
int maxDepth(TreeNode* root) {
if (root == nullptr) return ;
stack<tuple<TreeNode*, int>> sta;
int maxdeep = ;
int deep = ;
while(!sta.empty() || root){
while(root){
sta.push(make_tuple(root, deep+));
deep++;
root = root->left;
}
tie(root, deep) = sta.top();
if(maxdeep < deep) maxdeep = deep;
sta.pop();
root = root->right;
}
return maxdeep;
}
};
【BFS解法】
这个其实质就是层次遍历,使用队列来储存树节点,并使用size变量记录每次节点的数量,在一次循环中,处理掉一层的节点,具体做法:将某一层所有节点的子节点压入队列后,并将所有的节点移除队列。
在处理某一层的树节点的同时,使用count变量记录处理的层数。即为最大深度!
/**
* 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:
int maxDepth(TreeNode* root) {
if(root == nullptr){
return ;
}
queue<TreeNode*> que;
que.push(root);
int count = ;
while(que.size() != ){
int size = que.size(); while(size--){
TreeNode* tmp = que.front();
if(tmp->left != nullptr) que.push(tmp->left);
if(tmp->right != nullptr) que.push(tmp->right);
que.pop();
}
count++;
}
return count;
}
};
【LeetCode】二叉树的最大深度的更多相关文章
- LeetCode - 二叉树的最大深度
自己解法,欢迎拍砖 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null,nu ...
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode初级算法--树01:二叉树的最大深度
LeetCode初级算法--树01:二叉树的最大深度 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.n ...
- [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode(104):二叉树的最大深度
Easy! 题目描述: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例:给定二叉树 [3,9,20,null, ...
- [Leetcode] Maximum depth of binary tree二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode 腾讯精选50题--二叉树的最大深度
求二叉树的最大深度, 基本思路如下: 设定一个全局变量记录二叉树的深度,利用递归,没遍历一层都将临时深度变量+1,并在每一节点递归结束后判断深度大小. 具体代码如下: package algorith ...
- Java实现 LeetCode 104 二叉树的最大深度
104. 二叉树的最大深度 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nu ...
随机推荐
- angularJs-服务调用与后台数据获取
可以用factory做一些后台数据的获取,例如 happyFarm.factory('seedList',['$http',function($http){ return { ge ...
- Wireshark安装失败或找不到网络接口问题
Wireshark安装失败或找不到网络接口问题 Wireshark捕获数据包,主要依赖Winpcap或Npcap组件.从Wireshark 3.0开始,Npcap代替了Winpcap组件,成为Wire ...
- git使用问题一新建本地仓库添加远程合并推送
1,git远程新建仓库demo 2,git本地初始化仓库demo 3,git本地添加远程仓库 git remote add <name> <url> 4,git把远程仓库pul ...
- SQL中的Where,Group By,Order By和Having
说到SQL语句,大家最开始想到的就是他的查询语句: select * from tableName: 这是最简单的一种查询方式,不带有任何的条件. 当然在我们的实际应用中,这条语句也是很常用到的,当然 ...
- 2020寒假 05 ——eclipse安装scala环境
在eclipse中安装Scala环境 1安装eclipse插件步骤,点击help,选择Eclipse Marketplace 2.输入Scala,点击go 3.选择搜索到的Scala IDE 4.7. ...
- 《新标准C++程序设计》4.4(C++学习笔记14)
运算符重载为友元函数 一般情况下,将运算符重载为类的成员函数,是较好的选择. 但有时,重载为成员函数不能满足使用要求,重载为普通函数,又不能访问类的私有成员,所以需要将运算符重载为友元. class ...
- login SMTP send mail error : Unable to read data from the transport connection: net_io_connectionclosed
client.Credentials = new NetworkCredential(sSMTPuser, sSMTPpass); client.Credentials = new NetworkCr ...
- Git设置ssh密钥
前提条件是,公司的gitlab,运维没有关闭ssh的权限获取,或者叫运维开通那个ssh权限,生成的公钥要给运维那边一个!这样设置是可以成功 一.创建ssh key 1.在客户端查看有没有密钥 cd ~ ...
- C++获取文件夹中所有文件
获取文件夹中的文件,用到过很多次,每次用的时候都要去查下,很烦,所以想自己写下,当然,借鉴了很多其他大佬的博客 主要实现的函数,如下: void getFiles( string path, vect ...
- mvn 编译报错mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targ
mavn 编译报错: mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.p ...