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 ...
随机推荐
- spring简介
在SSH框假中spring充当了管理容器的角色.我们都知道Hibernate用来做持久层,因为它将JDBC做了一个良好的封装,程序员在与数据库进行交互时可以不用书写大量的SQL语句.Struts是用来 ...
- debian/ubuntu 下ISE安装
1. planAhead无法打开的问题 原因: debian中使用dash,跟planAhead使用的bash略有不同 解决: 将/bin/sh 的链接从dash改为bash 2. FPGA Edit ...
- Linux命令行小插曲
当命令行出错活出现<时,可使用 ctrl+c强制退出
- Ubuntu 12.04 改造指南
文章转自:http://www.lupaworld.com/article-217719-1.html 升级12.04已经有一段时间了.作为一个从08年就开始用Ubuntu的老用户,我觉得作为一个LT ...
- Python 深入理解yield
只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: for 取出alist的每一项,然后把i + 1塞进去.然后通过调用取出每一项: = [1, 2, 3, 4]for x in ...
- linux系统添加硬盘方法
[root@wen /]# fdisk -l[root@wen /]# fdisk /dev/sdb[root@wen /]# mkfs -t ext3 -c /dev/sdb1[root@wen / ...
- SQL Server里的闩锁介绍
在今天的文章里我想谈下SQL Server使用的更高级的,轻量级的同步对象:闩锁(Latch).闩锁是SQL Server存储引擎使用轻量级同步对象,用来保护多线程访问内存内结构.文章的第1部分我会介 ...
- 【Bugly干货分享】iOS内存管理:从MRC到ARC实践
Bugly 技术干货系列内容主要涉及移动开发方向,是由Bugly邀请腾讯内部各位技术大咖,通过日常工作经验的总结以及感悟撰写而成,内容均属原创,转载请标明出处. 对于iOS程序员来说,内存管理是入门的 ...
- Sensor(ACCELEROMETER)
package com.example.sensor01; import java.util.List; import android.hardware.Sensor; import android. ...
- 赴美工作常识(Part 5 - 绿卡优先级)
这个系列的第一篇<赴美工作常识(Part 1 - 签证)>是三年前写的,过了这么久这个系列终于要继续下去了.接下来当然时讨论绿卡的事情了.跟讨论签证一样,这里必须要有免责声名.因为我不是移 ...