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 ...
随机推荐
- 今天自己解决了两个问题(IE10 type slow ChromeDriver erro)
都是通过google解决的,其实本应该很快解决,可是因自己粗心,大写小错了,加上java基础不过关, "webdriver.chrome.driver"中的webdriver应是全 ...
- 练习1-21:编写程序entab,将空格串替换为最少数量的制表符和空格。。。(C程序设计语言 第2版)
#include <stdio.h> #define N 5 main() { int i, j, c, lastc; lastc = 'a'; i = j = ; while ((c=g ...
- Python 深入理解yield
只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: for 取出alist的每一项,然后把i + 1塞进去.然后通过调用取出每一项: = [1, 2, 3, 4]for x in ...
- Linux之源码包安装软件
安装准备 安装c语言编辑器 gcc 压缩包 node-v6.2.0-linux-x64.tar.gz 源码包保存位置 /usr/local/src/ 源码包安装位置 /us ...
- 酷狗 KRC 文件的解析
清理硬盘发现以前写过一个进行一半的代码,这次补全并从硬盘删掉. 格式说明来自 https://shansing.com/read/392/ krc解码并解压缩后得到一个字符串,例子: [id:$000 ...
- hadoop使用问题
前提 环境 ubuntu 安装hadoop 已经有一段时间 1.启动的时候提示 Connection reset by peer 这个查看日志,里面有说 ssh里面某个文件的权限太大 这个ssh里修改 ...
- Wix 安装部署教程(十六) -- 自动生成多语言文件
因为持续集成需要,所有项目编译完之后生成一个多语言的安装包.之前生成mst文件都是手动操作,而且mst文件必须每次重新和新的安装包“关联”,否则中文的安装包去调用英文的资源的时候就会报类似于“类型转换 ...
- JavaScript正则表达式下——相关方法
上篇博客JavaScript 正则表达式上——基本语法介绍了JavaScript正则表达式的语法,有了这些基本知识,可以看看正则表达式在JavaScript的应用了,在一切开始之前,看看RegExp实 ...
- 了解了这些才能开始发挥jQuery的威力
由于当前jQuery如此的如雷贯耳,相信不用介绍什么是jQuery了,公司代码中广泛应用了jQuery,但我在看一些小朋友的代码时发现一个问题,小朋友们使用的仅仅是jQuery的皮毛,只是使用id选择 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(1)
介绍 当涉及到计算机软件的开发时,我想运用所有的最新技术.例如,前端使用最新的 JavaScript 技术,服务器端使用最新的基于 REST 的 Web API 服务.另外,还有最新的数据库技术.最新 ...