【leetcode刷题笔记】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.
题解:递归,树的高度 = max(左子树高度,右子树高度)+1;
代码如下:
/**
* Definition for binary tree
* 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 left = maxDepth(root.left);
int right = maxDepth(root.right); return Math.max(left, right)+1;
}
}
被虐了一天,瞬间好受多了:-)
【leetcode刷题笔记】Maximum Depth of Binary Tree的更多相关文章
- [刷题] 104 Maximum Depth of Binary Tree
要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int va ...
- leetcode刷题-559. Maximum Depth of N-ary Tree
题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...
- [LeetCode&Python] Problem 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
题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- [leetcode刷题笔记]Implement Trie (Prefix Tree)
题目链接 一A,开森- ac代码: class TrieNode { // Initialize your data structure here. char content; boolean isW ...
- 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练习题】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 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 ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
随机推荐
- iphone开发技术要学习的内容
一.iOS基础 1 开发环境搭建以及IOS组件.框架的概要介绍. 2 mac操作系统与iOS操作系统 3 xcode IDE开发环境的初始 二.C语言基础 1数据类型.表达式与控制流程语句 2数组.函 ...
- ios 即时通讯 xmpp
[iPhone高级] 基于XMPP的IOS聊天客户端程序(XMPP服务器架构) http://blog.csdn.NET/kangx6/article/details/7739828 [iPhone高 ...
- drawRect setNeedsDisplay layoutSubViews
drawRect setNeedsDisplay layoutSubViews 1. drawRect: is invoked automaticall,never call it directl ...
- postman是如何使用的?
1.地址:https://segmentfault.com/a/1190000005055899
- 使用3DES+Base64来加密传输iOS应用数据
本文转载至 http://www.erblah.com/post/objective-c/shi-yong-3des-base64lai-jia-mi-chuan-shu-iosying-yong-s ...
- ubuntu14.04 desktop 32-bit kvm装windows xp
经过这几天来的折腾,总算是在ubuntu14.04用kvm装上了xp, 看不少的的贴,也绕了不少的圈,总的来说,非常感谢CSDN上的"上善若水75",看着他写的一个分类" ...
- iOS 微信支付点击左上角返回解决方案
在网了搜了一些解决方案,感觉并不是那么严谨,于是自己动手搞了一下,直接说思路 iOS调起第三方支付和安卓还不一样,安卓是把第三方的支付SDK直接镶嵌在自己的App中,而iOS由于沙盒机制,各个应用之间 ...
- 使用apt-get 安装后的mysql 登录问题
当使用apt-get安装mysql后,ubuntu会自动生成一个用户名和密码.所以在第一次登陆时会报如下错误 ERROR 1045 (28000): Access denied for user 'd ...
- 函数创建XML文件
REPORT YTST_XML_14. *----------------------------------------------------------------------* * PANT ...
- FlashFXP上传下载
正常情况我们的生产环境我们本地是不能直接访问的,因为网段不通,且生产环境不允许我们随便访问,但是对于我们自运维的集群我们有时候需要上去做一些操作,通过堡垒机跳转到生产机器上即可,但是我们不能通过xsh ...