LeetCode Maximum Depth of Binary Tree (求树的深度)
题意:给一棵二叉树,求其深度。
思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1。
/**
* 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 ;
int a=maxDepth(root->left);
int b=maxDepth(root->right);
return a>b?a+:b+;
}
};
AC代码
LeetCode Maximum Depth of Binary Tree (求树的深度)的更多相关文章
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- [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 ...
- [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 Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度
求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- [LeetCode] 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 Maximum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode:Maximum Depth of Binary Tree【Python版】
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
随机推荐
- .htaccess 设置
RewriteEngine on RewriteCond %{HTTP_HOST} ^blog.chosenet.com$RewriteCond %{REQUEST_URI} !^/blog/Rew ...
- sqlplus连接数据库的4种方式
本文对sqlplus连接数据库的几种方式进行大概介绍 下面是我的tnsnames.ora相关信息: /opt/oracle/product/10.1.0/db_1/network/admin/tnsn ...
- Redis之七种武器
长生剑.孔雀翎.碧玉刀.多情环.离别钩.霸王枪.拳头是古龙笔下的七种武器,而本文打算将Redis的几种使用方式 Strings.Hashs.Lists.Sets.Sorted Sets.Pub/Sub ...
- iOS10 权限崩溃问题-b
手机升级了 iOS10 Beta,然后用正在开发的项目 装了个ipa包,发现点击有关 权限访问 直接Crash了,并在控制台输出了一些信息: This app has crashed because ...
- hdu 4715 Difference Between Primes(素数筛选+树状数组哈希剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=4715 [code]: #include <iostream> #include <cstdio ...
- 使用Yeoman搭建 AngularJS 应用 (9) —— 让我们搭建一个网页应用
原文地址:http://yeoman.io/codelab/install-packages.html 列出当前程序包 我们现在查看一下我们已经安装的程序包,输入下面的命令 bower list 查找 ...
- 团体程序设计天梯赛-练习集L1-020. 帅到没朋友
L1-020. 帅到没朋友 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为 ...
- spoj 39
DP 完全背包问题 的 d[i] = min(d[i], d[i-w]+p) d[i]表示当总重量为i时可装的最小价值 #include <cstdio> #include &l ...
- SDUT 2523 OOXX
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2523 思路 :就是先统计一下方阵中1多少2多少 ...
- DJANGO输出HIGHCHARTS数据的样例
XXX,DJANGO ORM里确实有很深的水,需要慢慢理解.. 比如: 获取指定时间段的数据: app.deployversion_set.filter(add_date__range=(date_s ...