LintCode Maximum Depth of Binary Tree
1 /**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: An integer.
*/
public int maxDepth(TreeNode root) {
// write your code here
if (root == null) {
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left,right) + 1;
}
}
This question I used divide and conquer to do this question. I firstly divide this into left and right subproblems and then from the leaves to root return and +1;
LintCode Maximum Depth of Binary Tree的更多相关文章
- [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][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 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 —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 104. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- 【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 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
随机推荐
- TextInfo
https://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase(v=vs.110).aspx Co ...
- Intellij 图标介绍及配置文件常识
图标 参数名称 含义 默认值 -Xms 初始堆大小 物理内存的1/64(<1GB) 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,JVM就会增大堆直到-Xm ...
- 【hdu4366】dfs序线段树
#include <bits/stdc++.h> #define ll long long #define lson l, m, rt<<1 #define rson m+1, ...
- java高薪之路__006_多线程
线程的创建有两种方式 package learn.JavaBasics.Class; public class ThreadDemo extends Thread { private static i ...
- [java基础]循环结构2
[java基础]循环结构2 写了几个循环结构练习~记录一下~~ 1:99乘法表 /** 文件路径:G:\JavaByHands\循环语句\ 文件名称:GameForFor.java 编写时间:2016 ...
- 主成分分析(PCA)核心思想
参考链接:http://pinkyjie.com/2011/02/24/covariance-pca/ PCA的本质其实就是对角化协方差矩阵. PCA就是将高维的数据通过线性变换投影到低维空间上去,但 ...
- 关于H5中自定义属性的设置和获取
自定义数据属性是在HTML5中新加入的一个特性.简单来说,自定义数据属性规范规定任何以data-开头属性名并且赋值.自定义数据属性是为了保存页面或者应用程序的私有自定义数据,这些自定义数据属性保存进D ...
- Apriori原理与实践
Apriori: 其核心思想是通过候选集生成和情节的向下封闭检测两个阶段来挖掘频繁项集.经典的关联规则数据挖掘算法Apriori 算法广泛应用于各种领域,通过对数据的关联性进行了分析和挖掘,挖掘出的这 ...
- DEV GridControl.TableView FocusedRow选中行背景颜色
上次修改了TableView.RowStyle,导致了一个问题:覆盖了GridControl默认的选中行颜色. 于是需要重写选中行的颜色. 刚开始的想法是: <dxg:TableView> ...
- Windows7系统下JAVA运行环境下载、安装和设置(第二次更新:2012年03月14日)
1.下载 地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html,(由于Sun于2009年被oracle收购所以网址 ...