LeetCode OJ: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.
求二叉树深度,直接看代码:
/**
* 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 == NULL) return ;
_max = ;
tranverse(root->left, );
tranverse(root->right, );
return _max;
}
void tranverse(TreeNode * node, int count)
{
if(node == NULL)
return;
else{
if(count + > _max)
_max = count + ;
tranverse(node->left, count + );
tranverse(node->right, count + );
}
}
public:
int _max;
};
java版的如下所示,上面的递归写的还是太麻烦了,下面的简单很多哈:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
if(root == null)
return 0;
return tranverse(root, 0);
} public int tranverse(TreeNode root, int val){
if(root.left == null && root.right == null)
return val+1;
int leftDep = 0, rightDep = 0;
if(root.left != null)
leftDep = tranverse(root.left, val + 1);
if(root.right != null)
rightDep = tranverse(root.right, val + 1);
return Math.max(leftDep, rightDep);
}
}
LeetCode OJ:Maximum Depth of Binary Tree(二叉树最大深度)的更多相关文章
- 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二叉树的最大深度 C++/Java
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】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- Leetcode 104 Maximum Depth of Binary Tree 二叉树
计算二叉树的最大深度 我的方法是找出两个子树的长度中最长的那个,然后加1 class Solution { public: int maxDepth(TreeNode* root) { ; ,maxD ...
- [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] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [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】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- JavaScript你所不知道的困惑(3)
版权声明:本文出自水寒的原创文章.未经博主同意不得转载. https://blog.csdn.net/lxq_xsyu/article/details/25600011 困惑一: window.col ...
- 交叉熵(Cross-Entropy) [转载]
交叉熵(Cross-Entropy) 交叉熵是一个在ML领域经常会被提到的名词.在这篇文章里将对这个概念进行详细的分析. 1.什么是信息量? 假设X是一个离散型随机变量,其取值集合为X,概率分布函数为 ...
- 我的第二个Python小程序
输出0-100之间的偶数: # Author: fansik # Description: Output an even number between 0 and 100 # method one n ...
- R语言中abline和lines的区别
函数lines()其作用是在已有图上加线,命令为lines(x,y),其功能相当于plot(x,y,type="1")函数abline()可以在图上加直线,其使用方法有四种格式.( ...
- C#编写图书列表winform
Book.cs文件 using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...
- mysql数据库补充知识7 索引原理与慢查询优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- Spring_HelloWord
环境:IntelliJ 14 : jdk1.8 Spring操作步骤 1.新建项目---Spring Batch 2.IntelliJ会自动加载jar包 3.现在就可以在src目录下写Java类文 ...
- 父元素设置overflow,绝对定位的子元素会被隐藏或一起滚动
一般情况: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <met ...
- wampserver安装缺失vcruntime140.dll
wampserver安装缺失vcruntime140.dll,这是安装wamp时候经常遇到的一个问题,对于初学者来说很难解决,以前的百度经验很难解决,所以给大家一个可以用的. 方法/步骤 请先 ...
- 关于发邮件报错535 Error:authentication failed&553 authentication is required
553 authentication is required:这个错误的意思是你必须需要认证. 也就是说,你连接smtp服务器的时候必须使用密码来连接:下面代码红色那句 代码: @Override p ...