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 ...
随机推荐
- Leetcode OJ : Compare Version Numbers Python solution
Total Accepted: 12400 Total Submissions: 83230 Compare two version numbers version1 and version2 ...
- uva 2218 Triathlon
题意:铁人三项赛,给定每个选手游泳,自行车,赛跑三个阶段的平均速度,不知道每段比赛的路程,询问当前这个选手能否胜利. 思路:把题意转化为一个不等式,设比赛长度是1,如果i要战胜j,x.y分别是第一阶段 ...
- 【FOI】异或问题
题意: 给出n个数ai 和m个操作 操作有两种 C x y:将ax的值改为y Q x:求几种方案使得 b1^b2^...^bn=x(ai>=bi) 题解: 先推荐一个不错的题解 题目差不多 我之 ...
- HDU1890 Robotic Sort Splay tree反转,删除
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题目中涉及数的反转和删除操作,需要用Splay tree来实现.首先对数列排序,得到每个数在数列 ...
- Eclipse hangs on loading workbench, eclipse停在启动界面的处理办法
http://stackoverflow.com/questions/8972034/eclipse-hangs-on-loading-workbench 解答一: In most cases, rm ...
- 双MIC安卓手机录音问题
最近项目中解决了噪音问题和杂音问题后,用户向我们反馈(同时我们也发现)另外一个问题.部分直播间左右耳麦音量大小不一样,也就是左右声道音量大小不一样.找了几款反馈过来的手机,分别是荣耀3X.盖世3和联想 ...
- Java之序列流SequenceInputStream
序列流:作用就是将多个读取流合并成一个读取流,实现数据的合并 序列流表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到文件的末尾,接着从第二个输入流读取,以此类推:这样 ...
- 转载 SharePoint【Site Definition 系列】– 创建Content Type
转载原地址: http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...
- AndroidStudio快捷键汇总
最近开始全面转向Android Studio开发了,经常要去查快捷键,索性汇总下,自己方便查找 IDE 按键 说明 F1 帮助 Alt(Option)+F1 查找文件所在目录位置 Alt(Option ...
- 安装Intel HAXM为Android 模拟器加速,30秒内启动完成
要求 必备知识 windows 7 基本操作. 运行环境 windows 7(64位); Android Studio 1.1.0;JDK 1.7.0_75(64位);android-sdk_r24 ...