要求:求二叉树的深度(二叉树的深度为最远叶子节点到根节点的距离,即根节点到最远叶子节点的距离)

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.

有两种求解的思路,一种采用DFS的思想,一种采用BFS的思想,如下代码所示:

 struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x): val(x), left(NULL),right(NULL) {}
}; //采用DFS的思想
int maxDepth(TreeNode *root)
{
if (NULL == root)
return ;
int l = maxDepth(root->left);
int r = maxDepth(root->right); return l > r ? l + :r+;
//以上这两种方式有一种更简便的方法
//return 1 + max(maxDepth(root->left), maxDepth(root->right));
} //采用BFS的方法,引入队列
int maxDepth(TreeNode *root)
{
if (NULL == root)
return ;
queue <TreeNode *> que;
int nCount = ;
int nDepth = ;// 记录队列里面每一层上的元素 que.push(root);
while(!que.empty()) {
TreeNode *pTemp = que.front();
que.pop();
nCount --; if (pTemp->left)
que.push(pTemp->left);
if (pTemp->right)
que.push(pTemp->right); if (nCount == ) {
nDepth ++;
nCount = que.size();
}
}
return nDepth;
}

LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. [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 ...

  6. [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 ...

  7. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  8. LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. [LeetCode] 104. Maximum Depth of Binary Tree ☆(二叉树的最大深度)

    描述 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

随机推荐

  1. 外网访问Vmware虚拟机中的某个服务(如http)

    如果主机是windowx NAT中隐藏的端口映射,说明一下环境,利用当然是VMnet8网络连接,在虚拟机中架设linux WEB服务器利用WEB默认80端口,IP为192.168.11.10,真实主机 ...

  2. 绑定checkedComboBox

    using System; namespace CommonLib{ /// <summary> /// CommonCode 的摘要说明. /// </summary> [S ...

  3. Flask最强攻略 - 跟DragonFire学Flask - 第九篇 Flask 中的蓝图(BluePrint)

    蓝图,听起来就是一个很宏伟的东西 在Flask中的蓝图 blueprint 也是非常宏伟的 它的作用就是将 功能 与 主服务 分开怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查看 ...

  4. CF 317 A. Lengthening Sticks(容斥+组合数学)

    传送门:点我 A. Lengthening Sticks  time limit per test        1 second You are given three sticks with po ...

  5. Java12-java语法基础(十一)继承

    Java12-java语法基础(十一)继承 一.继承 学习要求: 1. 理解继承的概念与作用 2. 掌握继承的实现机制 3.理解继承中的覆写与覆盖 4.掌握super关键字的使用 回顾: 1.对客观世 ...

  6. 47-java 排列组合

    import java.util.HashSet; public class Main1 { public static int ys = 0; public static int ys2 = 0; ...

  7. springboot + @KafkaListener 手动提交及消费能力优化

    转载 https://blog.csdn.net/asd5629626/article/details/82776450  https://blog.csdn.net/asd5629626/artic ...

  8. protobuff 编译注意事项

    把protoc.exe增加到环境变量path,这样方便运行protoc 生成C++代码  protoc -I=Proto文件路径 –cpp_out=指定输出.h和.cc的目录 Proto文件 具体参数 ...

  9. 关于java中的一些循环

    1:switch语句 (1)格式: switch(表达式) { case 值1: 语句体1; break; case 值2: 语句体2; break; ... default: 语句体n+1; bre ...

  10. linux命令-crontab

    一.安装 yum install crontabs 二.基本使用 1.crontab -e:创建任务,进入编辑 格式: 基本格式 : ——————————————————— * * * * * com ...