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 longest path from the root node down to the farthest leaf node.
分析:求二叉树的最大深度
解法一:很容易想到的便是递归(深度优先搜索)
(1)如果根节点是空,则返回0;否则转到(2)
(2) l = 左子树的最大深度; r = 右子树的最大深度; 返回 max(l, r) + 1;
/**
* Definition for a binary tree node.
* 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 ;
int l = maxDepth(root->left);
int r = maxDepth(root->right);
return max(l, r) + ;
}
};
解法二:我觉得还可以用广度优先搜索:第i层如果存在节点不为空,则深度加1...最大深度就是这棵树的层数。
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL)
return ;
int depth = ;
queue<TreeNode*> node_que;
TreeNode* temp;
node_que.push(root);
while(!node_que.empty()){
int size = node_que.size();
while(size--){
temp = node_que.front();
node_que.pop();
if(temp->left != NULL)
node_que.push(temp->left);
if(temp->right != NULL)
node_que.push(temp->right);
}
depth++;
}
return depth; }
};
Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- Leetcode 104 Maximum Depth of Binary Tree 二叉树
计算二叉树的最大深度 我的方法是找出两个子树的长度中最长的那个,然后加1 class Solution { public: int maxDepth(TreeNode* root) { ; ,maxD ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- 104 Maximum Depth of Binary Tree 二叉树的最大深度
给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- [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 ...
- (二叉树 BFS DFS) 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 ...
随机推荐
- Excel2010: Excel使用小技巧(不断更新)
目录 1. 如何设置定时保存2. 调出“开发工具”功能区3. 在Excel单元格中输入对号和错号4. 设置单元格自动换行5. 向Excel中导入.txt文件6. 如何批量删除超链接7. 如何冻结窗格8 ...
- 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数
题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次. 分析:首先最先想到的是遍历从1到n的每 ...
- 5.1 CUDA atomic原子操作
和许多多线程并行问题一样,CUDA也存在互斥访问的问题,即当一个线程改变变量X,而另外一个线程在读取变量X的值,执行原子操作类似于有一个自旋锁,只有等X的变量在改变完成之后,才能执行读操作,这样可以保 ...
- HW6.19
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 100+经典Java面试题及答案解析
面向对象编程(OOP) Java是一个支持并发.基于类和面向对象的计算机编程语言.下面列出了面向对象软件开发的优点: 代码开发模块化,更易维护和修改. 代码复用. 增强代码的可靠性和灵活性. 增加代码 ...
- keil中 code、data、idata的区别
存储器类型 本C51编译器支持8051及其派生类型的结构能够访问8051的所有存储器空间具有下表列出的存储器类型的变量都可以被分配到某个特定的存储器空间.存储器类型 描述code 程序空间64 Kby ...
- 转载LINQ TO Entity 在数据库发生更改时更新实体数据模型 .edmx 文件
转载原出处:http://blog.csdn.net/litao2/article/details/8629335 在“模型浏览器”中,右击 .edmx 文件,然后选择“从数据库更新模型”. 模型更新 ...
- CSS3之边框图片border-image
CSS3中有关border的属性,还有很多,今天我将为大家介绍一个很好玩的属性——Border-image.有了CSS3之边框图片Border-image,我们可以轻松搞定圆角,轻松搞定很多之前难搞的 ...
- [五]SpringMvc学习-Restful风格实现
1.Restful风格的资源URL 无后缀资源的访问(csdn用法) 2.SpringMvc对Rest风格的支持 2.1将 /*.do改为/ 2.2 3.@PathVariable获取Url变量 @R ...
- Linux基本操作 9----- 认识与学习bash
一 认识bash这个shell 1 管理整个计算机硬件的其实就是操作系统的内核,这个内核是需要被保护的,所以我们一般用户就只能通过shell来跟内核通信,以让内核达到我们所想打到的工作. 2 只要能够 ...