LeetCode:Maximum Depth of Binary Tree_104
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.
【优质算法】
算法一:
public class Solution {
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int Left = maxDepth(root.left)+1;
int Right = maxDepth(root.right)+1;
if(Left>Right)
return Left;
else
return Right;
}
}
算法二:
public int maxDepth(TreeNode root) {
if(root == null) return 0;
return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
}
【题后反思】
这道题看似很难,看了答案没想到如此的简单。使用深度优先算法是大材小用,因为我们仅仅是要一个数字结果。
这道题的递归思想也是很简单的,但是我没有直接答出来...弱鸡
递归思想分析(以算法一为线索,个人愚见):


(图片转载自网络)
【递归都要给一个root节点,如一图中的10号节点。这时候我们要分两路,分别判断每一路的最大深度】。其实递归的就是【】内的内容。二叉树中每个节点都是root节点,(递)最后root节点便分解到页节点的空子节点,然后我们要(归),空子节点当然要返回0了,然后到了子树的分叉口时,我们要判断从分叉口这里分出区的到底那个最大,然后返回最大的。然后归的过程中不但重复判断,最后便得到了最大深度。
LeetCode:Maximum Depth of Binary Tree_104的更多相关文章
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- [LeetCode] 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 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] 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] Maximum Depth of Binary Tree dfs,深度搜索
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode Maximum Depth of Binary Tree (求树的深度)
题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...
- leetcode Maximum Depth of Binary Tree python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- leetcode:Maximum Depth of Binary Tree【Python版】
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...
- 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 ...
随机推荐
- 关于winform中*.exe.config中的appSettings的节点的读取与修改
//读取到这个节点 string file = System.Windows.Forms.Application.ExecutablePath; Configuration config = Conf ...
- LAMP自定义编译安装
httpd 2.4.4 + mysql-5.5.28 + php-5.4.13编译安装过程: 一.编译安装apache 1.解决依赖关系 httpd-2.4.4需要较新版本的apr和apr-util, ...
- linux-dns服务器搭建
1.先查看系统是否安装了bind rpm -qa|grep bind 2.如果没有安装则
- python 多线程和多进程基本写法
#coding=utf-8 def aJob(arg): """ 提供给多线程调用 """ import threading t = thr ...
- JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (2):JavaFX建立及程式碼說明 (转帖)
說明:就如同標題一樣,前端會用到JavaFX.Swing.Java Web Start.Google Map 的技術, 後端就是JDBC.Servlet的技術,以及我們會簽署認證jar檔案,這樣才可存 ...
- [后端人员耍前端系列]Bootstrap篇:30分钟快速掌握Bootstrap
一.引言 很久没有写过博客了,但是最近这段时间都没有闲着,接触了很多方面.比如一些前端框架和组件.还有移动开发React-Native.以及对.NET框架设计的一些重新认识.这些内容在接下来的时间都会 ...
- 【腾讯Bugly干货分享】揭秘:微信是如何用libco支撑8亿用户的
本文来自于腾讯bugly开发者社区,未经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/58203cfcd149ba305c5ccf85 作者:Leiffy 导语 lib ...
- Backbone源码解析(一):Event模块
Backbone是一个当下比较流行的MVC框架.它主要分为以下几个模块: Events, View, Model, Collection, History, Router等几大模块.它强制依赖unde ...
- 设计模式之美:Product Trader(操盘手)
索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Product Trader 的示例实现. 意图 使客户程序可以通过命名抽象超类和给定规约来创建对象. Product Trad ...
- 【译】AS3利用CPU缓存
利用CPU缓存 计算机有随机存取存储器RAM(译注:即我们常说的内存),但有更快形式的存储器.如果你希望你的应用程序的快速运行,你需要知道这些其他的存储器.今天的文章中讨论了它们,并给出了两个AS ...