LeetCode_Maximum Depth of Binary Tree
一.题目
Maximum Depth of Binary Tree
Total Accepted: 70693 Total Submissions: 156776My
Submissions
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.
二.解题技巧
Depth of Binary Tree一样,都是对二叉树进行递归。然后将子树的最大深度进行返回。终于得到这个树的最大深度。
三.实现代码
#include <iostream> /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ 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 0;
} int Result = 1;
int Left = maxDepth(root->left);
int Right = maxDepth(root->right); if (Left * Right)
{
Result += Left < Right? Right : Left;
}
else
{
Result += Right + Left;
} return Result;
}
};
四.体会


LeetCode_Maximum Depth of Binary Tree的更多相关文章
- [LeetCode] 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] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LintCode] 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][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
随机推荐
- hdu1181(变形课)
点击打开链接 Problem Description 呃......变形课上Harry碰到了一点小麻烦,由于他并不像Hermione那样可以记住全部的咒语而任意的将一个棒球变成刺猬什么的,可是他发现了 ...
- uva是崩了 吗,还是我太年轻?
刚刚提交了一道题,发现提交状态一直是in judge queue,去提交状态那里看了下,排在我20分钟前的也在in judge queue,不知道前面还有多少.顿时感到好无力......
- [IOS]mac以太网连接
今天玩了一下苹果一体机.感觉还是蛮不错的,只是.就是用以太网连接的时候遇到了一点问题.用这篇文章记录一下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/ ...
- hdu 3697 Selecting courses (暴力+贪心)
Selecting courses Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 62768/32768 K (Java/Others ...
- UVA - 11021 - Tribles 递推概率
GRAVITATION, n.“The tendency of all bodies to approach one another with a strengthproportion to the ...
- Mac OS下PHP开发环境的搭建——基于XAMPP和IntelliJ IDEA
简单记录一下在MacOS下,搭建PHP的开发环境吧.其实,从本质上来说,Mac对于PHP的支持还是很好的,默认带了PHP和Apache,但是由于前期对系统本身不熟悉,所以还是略微走了一些弯路--也就是 ...
- 英语发音规则---K字母
英语发音规则---K字母 一.总结 一句话总结: 1.K发[k]音? kind [kaɪnd] n. 种类 bike [baɪk] n. 自行车 skate [skeɪt] vi. 滑冰 make [ ...
- [十二省联考2019] 异或粽子 解题报告 (可持久化Trie+堆)
interlinkage: https://www.luogu.org/problemnew/show/P5283 description: solution: 显然有$O(n^2)$的做法,前缀和优 ...
- 常用相关linux命令
查看进程netstat -tnlp | egrep "(9097)" lsof -i:9097 ps -ef | grep kafka 观察句柄变化lsof -p $pid | w ...
- 本地PC安裝Centos 6.5 操作手冊
http://www.xlgps.com/article/130038.html 一.准备工作 1.下载Centos6.5 ISO文件 我在官网上下的6.5版本CentOS-6.5-x86_64-bi ...