(二叉树 BFS DFS) 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 longest path from the root node down to the farthest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its depth = 3.
-----------------------------------------------------------------------------------------------------------------------------
求二叉树的最大深度。
emmm,用bfs时,注意要用循环,要先遍历完一层,再遍历下一层。和leetcode111 Minimum Depth of Binary Tree几乎相似,只是少写了一行代码而已。
C++代码:
/**
* 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) {
queue<TreeNode*> q;
if(!root) return ;
q.push(root);
int res = ;
while(!q.empty()){
res++;
for(int i = q.size(); i > ; i--){ //必须写循环,否则在[3,9,20,null,null,15,7]时,会返回5。嗯,就是返回了二叉树的结点的个数。
auto t = q.front();
q.pop();
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
}
return res;
}
};
也可以用DFS,如果能够理解递归,就能够很好的理解DFS了。
C++代码:
/**
* 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) return ;
return + max(maxDepth(root->left),maxDepth(root->right));
}
};
还有一个递归,是自顶向下的递归
C++代码:
/**
* 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 answer = ;
int maxDepth(TreeNode* root) {
int depth = ;
DFS(root,depth);
return answer;
}
void DFS(TreeNode* root,int depth){
if(!root) return;
if(!root->left && !root->right) answer = max(answer,depth);
DFS(root->left,depth+);
DFS(root->right,depth+);
}
};
(二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree的更多相关文章
- (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- [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 (二叉树的最大深度)
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(二叉树的最大深度)
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 ☆(二叉树的最大深度)
描述 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- leetcode 104 Maximum Depth of Binary Tree(DFS)
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(二叉树最大深度) 解题思路和方法
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
随机推荐
- array_merge
1.array_merge 中有两个参数:将两个关联数组合并为一个数组 <?php $a1=array("a"=>"red","b&quo ...
- [西安交大附中集训] d6 删边(cip)
B. 删边(cip.cpp/in/out 1S/256M) 题面 给出一个没有重边和自环的无向图,现在要求删除其中两条边,使得图仍然保持连通. 你的任务是计算有多少组不合法的选边方案.注意方案是无序二 ...
- Nginx log_format
L11 nginx 官网的日志格式如下 log_format compression(自定义名称) '$remote_addr - $remote_user [$time_local] ' '&quo ...
- Nginx 如何处理上游响应的数据
陶辉93 一个非常重要的指令 proxy_buffer_size 指令限制头部响应header最大值 proxy_buffering 指令主要是指 上游服务器是否接受完完整包体在处理 默认是on 也就 ...
- GLSL 变量属性
1. attribute变量为这个attribute变量指定一个位置(用无符号值表示):glBindAttribLocation利用这个“位置”来指定需要传给shader里的attribue变量的数据 ...
- Xml文件汉化后改变代码页
源Xml文件代码页为ansi,汉化后要转换为UTF-8,在网上搜素了以下结果,但解释的不都清楚,我找了好一阵才找到相应的操作. 为防止自己以后忘了,补充如下: 经过这样的设置,生成的汉化xml文件 ...
- 微信小程序 canvas 字体自动换行(支持换行符)
微信小程序 canvas 自动适配 自动换行,保存图片分享到朋友圈 https://github.com/richard1015/News 微信IDE演示代码https://developers.w ...
- Git——Git的简单介绍【一】
官方网站 Git官网 https://git-scm.com/ GitHub https://github.com GitLab https://about.gitlab.com/ SVN https ...
- BZOJ3105 新Nim游戏 【拟阵】
题目分析: 我不知道啥是拟阵啊,但有大佬说线性基相关的都是拟阵,所以直接贪心做了. 题目代码: #include<bits/stdc++.h> using namespace std; ; ...
- 【XSY2190】Alice and Bob VI 树形DP 树剖
题目描述 Alice和Bob正在一棵树上玩游戏.这棵树有\(n\)个结点,编号由\(1\)到\(n\).他们一共玩\(q\)盘游戏. 在第\(i\)局游戏中,Alice从结点\(a_i\)出发,Bob ...