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 ...
随机推荐
- 从0.5开始学Java 零
作为新世纪的码农,掌握多门语言是必须的. 我先从java学起,工作又是做c# ,现在辞职了想重新捡回java的技术,所以写一个长篇 来给自己复习 ,题目就叫做从0.5开始学java. 核心内容就是 一 ...
- Java线程池主线程等待子线程执行完成
今天讨论一个入门级的话题, 不然没东西更新对不起空间和域名~~ 工作总往往会遇到异步去执行某段逻辑, 然后先处理其他事情, 处理完后再把那段逻辑的处理结果进行汇总的产景, 这时候就需要使用线程了. 一 ...
- cobaltstrike3.5使用记录
一.服务器搭建与连接(1)团队服务器上: sudo ./teamserver 服务器IP 连接密码 (VPS的话要写外网ip,并且可以进行端口映射,默认使用50050端口) 但是这个一关闭团队服务器也 ...
- UVa 679 Dropping Balls (例题 6-6)
传送门:https://uva.onlinejudge.org/external/6/p679.pdf 题意:在一颗结点带开关的完全二叉树上扔球,初始时开关为关闭状态,树的深度为D(1 <= D ...
- HDU2124 Repair the Wall(贪心)
Problem Description Long time ago , Kitty lived in a small village. The air was fresh and the scener ...
- css3制作导航栏
<!doctype html><html lang="en"><head> <meta charset="UTF-8&qu ...
- ffmpeg编译
CFLAGS=-g ./configure --enable-opengl --disable-yasm --enable-shared --enable-pic
- delphi 中sql的语法规范
1.引号配对: 这是在Delphi使用SQL语句时容易出错的地方,由于delphi规定在字符串中用两个西文的单引号“''”表示一个“'”,在拼装语句的时候就容易疏忽遗漏. Delphi里有个函数Q ...
- Leetcode016 3Sum Closest
public class S016 { //借鉴S015的思想,只是稍微有点慢 public int threeSumClosest(int[] nums, int target) { Arrays. ...
- Gentoo解决Windows双系统时间不同步的问题
升级后的gentoo不再使用/etc/conf.d/clock,而是使用/etc/conf.d/hwclock来设置和时间相关了. 在/etc/conf.d/hwclock文件中设置系统时间为本地时间 ...