Leetcode_104_Maximum Depth of Binary Tree
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41964475
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.
思路:
(1)题意为找到二叉树最大深度:即为从树根到叶子节点的最大路径。
(2)该题思路和遍历二叉树和寻找二叉树最短深度类似,可以参照二叉树按层次遍历实现http://blog.csdn.net/pistolove/article/details/41929059。
(3)为了求得二叉树最大深度,本文也是考虑运用二叉树按层次遍历的思想,二叉树遍历层次的次数即为二叉树的最大深度,这里很容易理解。
(4)主要的解题思路还是二叉树按层次遍历。本文只不过顺手把其拿过来使用罢了。希望对你有所帮助。谢谢。
算法代码实现如下所示:
//最大深度 public static int getDeep(TreeNode root){ if(root==null) return 0; int level = 0; LinkedList<TreeNode> list = new LinkedList<TreeNode>(); list.add(root); int first = 0; int last = 1; while(first<list.size()){ last = list.size(); while(first<last){ if(list.get(first).left!=null){ list.add(list.get(first).left); } if(list.get(first).right!=null){ list.add(list.get(first).right); } first++; } level++; } return level; }
Leetcode_104_Maximum Depth of Binary Tree的更多相关文章
- [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [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 ...
- [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 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 ...
随机推荐
- 硬盘存储计量单位KB、MB、GB大小换算
一. 预备知识 1. bit与byte 1. bit(简记为 b) 1 bit = 0 or 1 = one binary 2. byte(简记为 B) 1 byte = 8 bits 1字节,8个二 ...
- Java中使用long类型实现精确的四则运算
引子 Effective Java 2nd Edition 第48条建议:如果需要精确的答案,请避免使用float和doble.float和double类型主要是为了科学计算和工程计算而设计的.他们执 ...
- Linux基础指令
Linux基础指令 只写了最简单的一些文件操作,基本没有带参数 查看当前目录 pwd 跳转到某路径 cd 查看当前目录下的文件 ls ls -l // -l 查看详细信息 打开当前所在文件夹 open ...
- Redis 学习笔记1:CentOS 6.7下安装Redis
在linux环境搭建Redis环境,首先从官网(http://redis.io/)下载Redis 版本,本人使用的3.21版本. 1. 将redis 解压到 /usr/local目录下. [root ...
- SQL Server 索引维护(1)——系统常见的索引问题
前言: 在很多系统中,比如本人目前管理的数据库,索引经常被滥用,甚至使用DTA(数据库引擎优化顾问)来成批创建索引(DTA目前个人认为它的真正用处应该是在发现缺失的统计信息,在以前的项目中,用过一次D ...
- 使用jQuery AJAX读取二进制数据
READING BINARY DATA USING JQUERY AJAX http://www.henryalgus.com/reading-binary-files-using-jquery-aj ...
- UNIX环境高级编程——实现uid to name
setpwent()用来将getpwent()的读写地址指回文件开头,即从头读取密码文件中的账号数据. strcut passwd * getpwent(void); getpwent()用来从密码文 ...
- 《java入门第一季》之对文件和字符串进行MD5加密工具类
上一篇介绍了MD5加密算法,之前写的代码有些冗余,而且可读性很差.今天把对文本数据的加密,以及获取文件的md5值做一个封装类.代码如下: package com.itydl.utils; import ...
- 【java集合框架源码剖析系列】java源码剖析之HashMap
前言:之所以打算写java集合框架源码剖析系列博客是因为自己反思了一下阿里内推一面的失败(估计没过,因为写此博客已距阿里巴巴一面一个星期),当时面试完之后感觉自己回答的挺好的,而且据面试官最后说的这几 ...
- 【原创】Nginx+PHP-FPM优化技巧总结
php-fpm的安装很简单,参见PHP(PHP-FPM)手动编译安装.下面主要讨论下如何提高Nginx+Php-fpm的性能. 1.Unix域Socket通信 之前简单介绍过Unix Domain S ...