一、     题目

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

二、     分析

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

找出最深路径,则每经过一个节点要加上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. Linux------创建和终止进程

    创建进程: Linux创建两个步骤的新处理:fork()和exec().其中fork创建当前进程的能力(父进程)副本,那个孩子.父子进程只有PID不同.在这之后,该系统具有两个进程,运行相同的操作.父 ...

  2. ActiveMQ相关背景(转)

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

  3. POJ1163 The Triangle 【DP】

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36918   Accepted: 22117 De ...

  4. Effective C++规定45 附加代码

    这部分是额外的代码的博客.键45条款想法已经实现. #include<iostream> using namespace std; template<typename T> c ...

  5. bellman_ford算法

    给定一个源点,求最短路径,那么存在以源点为根的最短路径树因为最短路径具有最优子结构的性质,所以我们可以先求出树的第一层,然后再求出树的第二层,以此类推bellman_ford算法就是按照这种思想求最短 ...

  6. C语言中main函数的參数具体解释

    main函数的定义形式         main函数能够不带參数,也能够带參数,这个參数能够觉得是 main函数的形式參数.C语言规定main函数的參数仅仅能有两个,习惯上这两个參数写为argc和ar ...

  7. UVA11100- The Trip, 2007

    option=com_onlinejudge&Itemid=8&category=512&page=show_problem&problem=2041"> ...

  8. android studio学习

    http://blog.csdn.net/ryantang03/article/details/8948037 http://www.it165.net/pro/html/201109/676.htm ...

  9. 重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree

    原文:重新想象 Windows 8 Store Apps (17) - 控件基础: Measure, Arrange, GeneralTransform, VisualTree [源码下载] 重新想象 ...

  10. Linux下一个php+mysql+nginx构建编译(三)

    在此之前一直是一个关键构建webserver.但一个关键的建筑环境都比较旧的.假定使用一个相对较新的环境,尤其是正式的server.您必须手动编译自己建(基于以下的结构linux centos6.5 ...