一、     题目

给定一个二叉树,求它的最大深度。最大深度是沿从根节点,到叶节点最长的路径。

二、     分析

(做到这里发现接连几道题都是用递归,可能就是由于自己时挑的简单的做的吧。)

找出最深路径,则每经过一个节点要加上1,当遇到空节点时,返回0。

在网上看了看有的做法除了麻烦点可是非常不错的方法,比方使用栈和队列等

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root==NULL)
return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
}; 队列 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
//二叉树最大深度(层次遍历,遍历一层高度加1)
int maxDepth(TreeNode *root) {
int height = 0,rowCount = 1;
if(root == NULL){
return 0;
}
//创建队列
queue<treenode*> queue;
//加入根节点
queue.push(root);
//层次遍历
while(!queue.empty()){
//队列头元素
TreeNode *node = queue.front();
//出队列
queue.pop();
//一层的元素个数减1,一层遍历完高度加1
rowCount --;
if(node->left){
queue.push(node->left);
}
if(node->right){
queue.push(node->right);
}
//一层遍历完
if(rowCount == 0){
//高度加1
height++;
//下一层元素个数
rowCount = queue.size();
}
}
return height;
} };</treenode*> 栈 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root == NULL) return 0; stack<treenode*> S; int maxDepth = 0;
TreeNode *prev = NULL; S.push(root);
while (!S.empty()) {
TreeNode *curr = S.top(); if (prev == NULL || prev->left == curr || prev->right == curr) {
if (curr->left)
S.push(curr->left);
else if (curr->right)
S.push(curr->right);
} else if (curr->left == prev) {
if (curr->right)
S.push(curr->right);
} else {
S.pop();
}
prev = curr;
if (S.size() > maxDepth)
maxDepth = S.size();
}
return maxDepth;
}
};
</treenode*> DFS
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root == NULL)return 0;
int res = INT_MIN;
dfs(root, 1, res);
return res;
}
void dfs(TreeNode *root, int depth, int &res)
{
if(root->left == NULL && root->right == NULL && res < depth)
{res = depth; return;}
if(root->left)
dfs(root->left, depth+1, res);
if(root->right)
dfs(root->right, depth+1, res);
}
}; 层次遍历 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
//层次遍历树的层数,NULL为每一层节点的切割标志
if(root == NULL)return 0;
int res = 0;
queue<TreeNode*> Q;
Q.push(root);
Q.push(NULL);
while(Q.empty() == false)
{
TreeNode *p = Q.front();
Q.pop();
if(p != NULL)
{
if(p->left)Q.push(p->left);
if(p->right)Q.push(p->right);
}
else
{
res++;
if(Q.empty() == false)Q.push(NULL);
}
}
return res;
}
};

Leetcode:maximum_depth_of_binary_tree题解的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. Leetcode 简略题解 - 共567题

    Leetcode 简略题解 - 共567题     写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...

  3. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  4. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  5. LeetCode一句话题解

    深度优先搜索 人生经验 1. 需要输出所有解.并由于元素集有重复元素,要求返回的结果需要去重的情况,可考虑使用值对应数量的map,然后分别考虑依次取不同数量该值的可能. LeetCode39 题目:给 ...

  6. [leetcode] 位操作题解

    子集 题目[78]:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 示例: 输入: nums = [1,2,3] 输出: [ [3],   [1],   [2],   [ ...

  7. LeetCode 中等题解(3)

    34 在排序数组中查找元素的第一个和最后一个位置 Question 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂 ...

  8. LeetCode 中等题解(1)

    16 最接近的三数之和 Question 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和. ...

  9. leetcode个人题解——two sum

    这是leetcode第一题,通过较为简单. 第一题用来测试的,用的c,直接暴力法过, /** * Note: The returned array must be malloced, assume c ...

随机推荐

  1. Mina框架断包、粘包问题解决方式

    Mina框架断包.粘包问题解决方式 Apache Mina Server 是一个网络通信应用框架,也就是说,它主要是对基于TCP/IP.UDP/IP协议栈的通信框架(当然.也能够提供JAVA 对象的序 ...

  2. Cocos2d-x学习笔记(1)

    Cocos2d-x原型Cocos2d,基于Cocos2d-iPhone,跨平台. Hello Workd分析: 1."resource"目录 该目录主要用于存放游戏中须要的图片.音 ...

  3. 带鉴权信息的SIP呼叫

    带鉴权信息的SIP呼叫 INVITE sip:1000@192.168.50.34SIP/2.0 Via: SIP/2.0/UDP192.168.50.32:2445;branch=z9hG4bK-d ...

  4. ActiveMQ相关背景(转)

    概述 介绍中间件.MOM.JMS.ActiveMQ,及相互的关系. 中间件 由于业务的不同.技术的发展.硬件和软件的选择有所差别,导致了异构组件或应用并存的局面.要使这些异构的组件协同工作,一个有效的 ...

  5. 数据库的group by 分组

    有一个表 查询结果为 用下面的代码写 select COUNT( case NumName when 'a' then NumName end ) as 'aaa', COUNT( case NumN ...

  6. 使用C++名单在文档处理和学生成绩管理系统相结合

    对于学生成绩管理系统,我并不陌生,几乎学习C人的语言.做项目会想到学生成绩管理系统,我也不例外.在研究中的一段时间C语言之后,还用C语言到学生管理系统,然后做几个链接.计数,这个系统是以前的系统上的改 ...

  7. Ext4功能和文件系统的简单功能

    Linux kernel 自 2.6.28 開始正式支持新的文件系统 Ext4. Ext4 是 Ext3 的改进版,改动了 Ext3 中部分重要的数据结构,而不只像 Ext3 对 Ext2 那样,不过 ...

  8. FZUOJ Problem 2178 礼品配送

    Problem 2178 礼物分配 题目链接: Click Here~ Problem Description 在双胞胎兄弟Eric与R.W的生日会上,他们共收到了N个礼物,生日过后他们决定分配这N个 ...

  9. Gitclient使用

    1 首次安装gitclient msysgit watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3h4NTA0NjU5OTg3/font/5a6L5L2T/ ...

  10. HDU4309-Seikimatsu Occult Tonneru(最大流)

    Seikimatsu Occult Tonneru Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...