【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度
求二叉树的最大深度
/**
* 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) {
int l,r;
if (root == NULL) return ;
l = maxDepth(root->left);
r = maxDepth(root->right);
return max(l,r)+; //一行代码的写法!!!!
//return root == NULL ? 0 : max(maxDepth(root -> left), maxDepth(root -> right)) + 1;
}
};
非递归写法1(直接摘自其他博客):
int maxDepth(TreeNode *root)
{
if (root == NULL) return ;
stack<TreeNode *> gray;
stack<int> depth;
int out = ; gray.push(root);
depth.push();
while (!gray.empty()) {
TreeNode *tmp = gray.top();
int num = depth.top();
gray.pop();
depth.pop();
if (tmp->left == NULL && tmp->right == NULL) {
out = num > out ? num : out;
}
else {
if (tmp->left != NULL) {
gray.push(tmp->left);
depth.push(num + );
}
if (tmp->right != NULL) {
gray.push(tmp->right);
depth.push(num + );
}
}
}
return out;
}
非递归写法2(直接摘自其他博客):
int maxDepth(TreeNode *root)
{
if(root == NULL)
return ; int res = ;
queue<TreeNode *> q;
q.push(root);
while(!q.empty())
{
++ res;
for(int i = , n = q.size(); i < n; ++ i)
{
TreeNode *p = q.front();
q.pop(); if(p -> left != NULL)
q.push(p -> left);
if(p -> right != NULL)
q.push(p -> right);
}
} return res;
}
【easy】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. 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 ...
- 104. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
随机推荐
- 定时任务 cron命令
一名linux新手,因为项目需要现在在学习linux操作系统. 因项目需要做一个定时任务,每个小时访问某一个url一次,通过参考很多大神的博客学会了crontab命令的使用方法 crontab -u ...
- HTML之表格
表格标签和表格标题 表格标签 <table> <tr> <td></td> ...... </tr> <tr> <td&g ...
- AI佳作解读系列(一)——深度学习模型训练痛点及解决方法
1 模型训练基本步骤 进入了AI领域,学习了手写字识别等几个demo后,就会发现深度学习模型训练是十分关键和有挑战性的.选定了网络结构后,深度学习训练过程基本大同小异,一般分为如下几个步骤 定义算法公 ...
- c指针作业(第一次)
1. 数据类型的本质是什么? (从编译器的角度考虑) 数据类型可理解为创建变量的模具:是固定内存大小的别名 数据类型的作用:编译器预算对象(变量)分配的内存空间大小 注意:数据类型只是模具,编译器并没 ...
- Java基础:Java变量、数据类型、运算符(2)
1. 标识符和关键字 1.1 标识符 标识符是用来标识类名.对象名.变量名.方法名.类型名.数组名.文件名的有效序列. Java规定,标识符由字母.数字.下划线“_”.美元符号“$”组成,并且首字母不 ...
- 从源码看springboot默认的资源文件和配置文件所在位置
首先,使用的springboot版本是2.X,在这里写一点学习springboot的记录 springboot需要配置的不多,但也并不是完全不需要配置就可以顺畅使用,这里看一下它默认的配置 首先,看一 ...
- Python——字典操作
一.取出字典中所有的key-value student={'name':'xiaoming','age':11,'school':'tsinghua'} for key,value in studen ...
- Python学习之路——迭代器
迭代器 # 通过迭代器取值优缺点: # 优点:不依赖索引,完成取值 # 缺点:不能计算长度,不能指定位取值(只能从前往后逐一取值) 可迭代对象 ''' 可迭代对象: 有__iter__()方法的对象, ...
- BZOJ5341[Ctsc2018]暴力写挂——边分治+虚树+树形DP
题目链接: CSTC2018暴力写挂 题目大意:给出n个点结构不同的两棵树,边有边权(有负权边及0边),要求找到一个点对(a,b)满足dep(a)+dep(b)-dep(lca)-dep'(lca)最 ...
- Python3 与 C# 并发编程之~ 协程篇
3.协程篇¶ 去年微信公众号就陆陆续续发布了,我一直以为博客也汇总同步了,这几天有朋友说一直没找到,遂发现,的确是漏了,所以补上一篇 在线预览:https://github.lesschina.c ...