【Maximum Depth of Binary Tree 】cpp
题目:
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.
代码:
/**
* 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 ;
if ( !root->left && !root->right ) return ;
return std::max(Solution::maxDepth(root->left)+, Solution::maxDepth(root->right)+);
}
};
tips:
比求最小叶子节点深度容易一些。
========================================
第二次过这道题,一次AC。
/**
* 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(Solution::maxDepth(root->left), Solution::maxDepth(root->right))+;
}
};
【Maximum Depth of Binary Tree 】cpp的更多相关文章
- 【二叉树的递归】02二叉树的最大深度【Maximum Depth of Binary Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...
- 【Minimum Depth of Binary Tree】cpp
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- 【二叉树的递归】01二叉树的最小深度【Minimum Depth of Binary Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,找出他的最小的深度 ...
- 【LeetCode练习题】Maximum Depth of Binary Tree
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the ...
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...
- 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 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 ...
- 2016.6.26——Maximum Depth of Binary Tree
Maximum Depth of Binary Tree 本题收获 1.树时使用递归 2.注意边界条件时输出的值,仔细阅读题意,若是面试时,问清边界条件. 题目: Given a binary tre ...
随机推荐
- VS2013添加NuGet的方法
1.工具->扩展和更新->联机 2.右上角搜索框搜索NuGet,选择NuGet Package Manager for Visual Studio 2013,安装后重启VS 下面通过添加N ...
- 首页banner特效
<link href="css/swiper.min.css" rel="stylesheet" /> <script src=" ...
- dedecms 调用channel子栏目的id问题
dedecms 说明文档:http://www.dedecms.com/archives/templethelp/help/taghelp.htm {dede:channel type='son' t ...
- WinRt BottomAppBar
BottomAppBarDemo.xaml <Page.BottomAppBar> <AppBar> <StackPanel Orientation="Hori ...
- POJ C程序设计进阶 编程题#4:Tomorrow never knows?
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 甲壳虫的<A day ...
- c#获取多个List<class>合并、并将相同条件下的值累计sum
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Linux redis 配置文件
# Redis configuration file example # Note on units: when memory size is needed, it is possible to sp ...
- 学习c的第7天
#include <stdio.h> int main() { int x=0; if (x==0) { printf("x为假\n"); } else { print ...
- js闭包理解实例小结
Js闭包 闭包前要了解的知识 1. 函数作用域 (1).Js语言特殊之处在于函数内部可以直接读取全局变量 <script type="text/javascript"> ...
- C# 平时碰见的问题【4】
1. 模糊查询 like的参数化写法 string keyword="value"; // 要模糊匹配的值 错误示范: sql: string strSql=" ...