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 ...
随机推荐
- maven里的modelVersion
modelVersion 描述这个POM文件是遵从哪个版本的项目描述符
- Swift-ImageView响应点击事件
随着Swift语言的不断更新迭代,纯Swift语言编写的代码更加紧凑简单,结合StoryBorad的使用,使开发苹果APP的门槛降低了不少.个人也是比较推荐使用Interface Builder去生成 ...
- 图片下载缓存防止OOM
一 ImageManager ImageMemoryCache(内存缓存).ImageFileCache(文件缓存) 关于Java中对象的软引用(SoftReference),如果一个对象具有 ...
- .net笔记
一.垃圾回收 1.运行.NET应用程序时,程序创建出来的对象都会被CLR跟踪, 2.哪些对象还会被用到(存在引用关系):哪些对象不会再被用到(不存在引用关系),CLR都是有记录的. 3.CLR会整理不 ...
- 遭遇AutoMapper性能问题:映射200条数据比100条慢了近千倍
今天遇到了AutoMapper的一个性能问题,使用的是AutoMapper的Project特性,AutoMapper版本是3.3.0,代码如下: return await _repository .G ...
- 用“MEAN”技术栈开发web应用(一)AngularJs前端架构
前言 不知何时突然冒出“MEAN技术栈”这个新词,听起来很牛逼的样子,其实就是我们已经熟悉了的近两年在前端比较流行的技术,mongodb.express.angularjs.nodejs,由于这几项技 ...
- 翻译-高效DevOps的10项实践
原文链接: http://www.drdobbs.com/architecture-and-design/top-10-practices-for-effective-devops/240149363 ...
- 第二章 深入 C# 数据类型
第二章 深入 C# 数据类型 1.封装又称信息隐藏,是指利用抽象数据类型将数据和数据的操作结合在一起,使其构成一个不可分割的独立实体,尽可能的隐藏内部的细节,只保留一些对外接口,使之于外部发生联系. ...
- Java线程:线程的交互
一.线程交互的基础知识 SCJP所要求的线程交互知识点需要从java.lang.Object的类的三个方法来学习: void notify() 唤醒在此对象监视器上等 ...
- Atitit 发帖机系列(7) 词法分析的方法attilax大总结)
Atitit 发帖机系列(7) 词法分析的方法attilax大总结) 1.1. 词法分析貌似俩大方法,一个直接根据状态图转换,一个根据dfa1 1.2. switchcase或者ifelse 最原始方 ...