LeetCode OJ 104. 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.
* 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; int leftmax = maxDepth(root.left);
int rightmax = maxDepth(root.right); return leftmax>rightmax?leftmax+1:rightmax+1;
}
}
LeetCode OJ 104. Maximum Depth of Binary Tree的更多相关文章
- 【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】#104. Maximum Depth of Binary Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
- 【LeetCode OJ】Maximum Depth of Binary Tree
Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...
- 【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 OJ: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(easy)
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- LeetCode之104. Maximum Depth of Binary Tree
-------------------------------- 递归遍历即可 AC代码: /** * Definition for a binary tree node. * public clas ...
- 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 ...
随机推荐
- 找工作---操作系统常考知识点总结【PB】
1.进程是并发过程中程序的执行过程 2.进程的特征:结构特征.动态性.并发性.独立性.异步性 3.临界区指在每个进程中访问临界资源的那段代码 4,现在操作系统中申请资源的基本单位是进程,在CPU得到执 ...
- OvS: data structure analysis
hmap usage: in include/openvswitch/shash.h, we have: at first glance, it is a hmap encapsulated in s ...
- URAL 6089 Nine
水题,找误差范围之内9最多的时间,如果有多个,选择误差最小的一个,如果还有多个,选择字典序最小的一个.同一个时间可以有不同的表示方法,例如60:15也可以表示为59:75. #include<s ...
- 第一百零七节,JavaScript基本包装类型,数据类型的方法
JavaScript基本包装类型,数据类型的方法 学习要点: 1.基本包装类型概述 2.Boolean类型 3.Number类型 4.String类型 为了便于操作基本类型值,ECMAScript提供 ...
- js--事件对象的理解2
实例1:一串跟着鼠标飞舞的div <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- java中字符串的操作
//创建一个字符数组 char[] charArr = {'a','b','c','d','e','f','g'}; //创建一个字符串 String str = new String(charArr ...
- Eclipse版本区别
各版本的区别: Eclipse IDE for Java Developers 是Eclipse的platform加上JDT插件,用来java开发的 Eclipse IDE for Java EE D ...
- 遍历(二)javascript的Foreach语法
原文:http://www.cnblogs.com/Fskjb/archive/2011/03/26/1996165.html 首先,虽然叫foreach语法但关键字还是用for哦,这个语法只是对平时 ...
- POJ 1082 Calendar Game(找规律博弈)
传送门 以下复制自此处:http://www.xuebuyuan.com/2028180.html 博弈论题目可以用寻找必败状态的方法解决. 第一个必败状态是2001.11.04.由此可以推出其他任何 ...
- Java 反射 Array动态创建数组
Java 反射 Array动态创建数组 @author ixenos 注:java.lang.reflect.Array 是个反射工具包,全是静态方法,创建数组以多维数组为基准,一维数组只是特殊实现 ...