【一天一道LeetCode】#104. Maximum Depth of Binary Tree
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
来源:https://leetcode.com/problems/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.
(二)解题
题目大意:求二叉树的最大深度
解题思路:采用深度优先搜索,很容易求出最大深度
/**
* 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 max;//用来保存最大深度值
int maxDepth(TreeNode* root) {
max = 0;
dfsTree(root,0);//深度优先搜索递归
return max;
}
void dfsTree(TreeNode* root , int dep)
{
if(dep>max) max=dep;//记录最大深度值
if(root==NULL) return;
dfsTree(root->left,dep+1);//遍历左子树
dfsTree(root->right,dep+1);//遍历右子树
}
};
【一天一道LeetCode】#104. Maximum Depth of Binary Tree的更多相关文章
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- 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 二叉树的最大深度
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 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
Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...
- leetcode 104 Maximum Depth of Binary Tree ----- java
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Java [Leetcode 104]Maximum Depth of Binary Tree
题目描述: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...
- 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 python
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
随机推荐
- 关于惠普hp服务器开机时F10菜单变成F10 Function Disabled的解决方法
今天笔者由于在Intelligent Provisioning智能配置里不小心将"启动Intelligent Provisioning"选项钩选成禁用了,如下 结果就造成,在之后服 ...
- gulp填坑记(一)
gulp是基于Node.js的自动任务运行器.可以自动完成html.image.css和js等文件的检测.检查.合并.压缩.格式化等,并监听文件在改动后重复指定的这些步骤. 一.首先,我全局安装了gu ...
- 使用CSS让多出来的字变为省略号
<style> .text1 { width:200px; overflow:hidden; text-overflow:ellipsis; -o-text-overflow:ellips ...
- 【转】动态规划DP
[数据结构与算法] DP 动态规划 介绍 原创 2017年02月13日 00:42:51 最近在看算法导论. DP全称是dynamic programming,这里programming不是编程,是一 ...
- MySQL EXTRACT() 函数
定义和用法 EXTRACT() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 语法 EXTRACT(unit FROM date) date 参数是合法的日期表达式.unit 参 ...
- 【机器学习】从SVM到SVR
注:最近在工作中,高频率的接触到了SVM模型,而且还有使用SVM模型做回归的情况,即SVR.另外考虑到自己从第一次知道这个模型到现在也差不多两年时间了,从最开始的腾云驾雾到现在有了一点直观的认识,花费 ...
- Android进阶 | 摆脱斗图 | 行业交流 | 深度学习 | 付费
Android进阶 | 摆脱斗图 | 行业交流 | 深度学习 | 付费 其实在很早的时候我就有想过,是不是退出一些群,因为群太多了,里面的水友也多,基友也多,就难免会水起来,这样既耽误学习又耽误工作, ...
- ubuntu ssh 防止登陆断开
client 端: echo -e '\nServerAliveInterval 30' >> ~/.ssh/ssh_config server 端: echo -e '\nClientA ...
- webpack 命令行 传入自定义变量
https://github.com/webpack/webpack/issues/2254 --env 变量 Yes this is intended. Custom argumens can be ...
- SimpleDateFormat中parse和format的区别
parse()返回的是一个Date类型数据,format返回的是一个StringBuffer类型的数据 //SimpleDateFormat中的parse方法可以 //把String型的字符串转换成特 ...