【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree

解法一:递归
int maxDepth(TreeNode* root)
{
if (root == NULL)
return ; int max_left_Depth = maxDepth(root->left);
int max_right_Depth = maxDepth(root->right);
return max(max_left_Depth, max_right_Depth) + ;
}
解法二:BFS
int maxDepth(TreeNode* root)
{
if (root == NULL)
return ; queue<TreeNode*> node_queue;
node_queue.push(root); int count = ;
while (!node_queue.empty()) {
count++;
int len = node_queue.size();
for (int i = ; i < len; ++i) {
TreeNode *node = node_queue.front();
node_queue.pop(); if (node->left)
node_queue.push(node->left);
if (node->right)
node_queue.push(node->right);
}
}
return count;
}
【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree的更多相关文章
- LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- [Swift]LeetCode104. 二叉树的最大深度 | 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】【Easy】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刷题笔记】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二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- 【LeetCode练习题】Maximum Depth of Binary Tree
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- [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 ...
随机推荐
- Python笔记 #01# Convert Python values into any type
源:DataCamp datacamp 的 DAILY PRACTICE + 日常收集. How much is your $100 worth after 7 years? Guess the t ...
- Python面试题之Python正则表达式re模块
一.Python正则表达式re模块简介 正则表达式,是一门相对通用的语言.简单说就是:用一系列的规则语法,去匹配,查找,替换等操作字符串,以达到对应的目的:此套规则,就是所谓的正则表达式.各个语言都有 ...
- CSS Tooltip(提示工具)
CSS Tooltip(提示工具) 提示工具在鼠标移动到指定元素后触发,可以在四个方位显示:头部显示.右边显示.左边显示.底部显示 一.基础提示框(Tooltip) 提示框在鼠标移动到指定元素上显示: ...
- 分享基于Sails.js和React.js的全栈聊天室
在线地址: http://fiora.suisuijiang.com/ 项目源码[ 路过求star(^_^) ]: 后端: https://github.com/yinxin630/fiora-bac ...
- axios配置大全
一.安装 1. 利用npm安装npm install axios --save 2. 利用bower安装bower install axios --save 3. 直接利用cdn引入<scrip ...
- 20145216史婧瑶《Java程序设计》第5周学习总结
20145216 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 8.1 语法与继承架构 Java中所有错误都会被打包为对象,运用try.catch,可以在错误发生 ...
- openwrt生成的镜像放在哪里
答:1.打包好之后是放在build_dir/target-$(cross-compile-toolchan-name)/linux-$(chip-series-name)_$(chip-arch)/t ...
- yum的使用
Yum官网 Yum的缓存 Yum仓库 Yum命令 Yum插件 yum的缓存 通过缓存可以提升我们环境的搭建效率,直接把原来缓存好的安装包数据放到新环境,省去了几个G的下载,甚至,有些时候客户现场不能联 ...
- Mybatis中使用自定义的类型处理器处理枚举enum类型
知识点:在使用Mybatis的框架中,使用自定义的类型处理器处理枚举enum类型 应用:利用枚举类,处理字段有限,可以用状态码,代替的字段,本实例,给员工状态字段设置了一个枚举类 状态码,直接赋值给对 ...
- webservice用cxf发布SOAP
cxf的安装,就是把文件解压,然后配置环境变量 http://cxf.apache.org/download.html这是官网下载 解压到这里 环境变量 wsdl2java命令测试 1.新建java项 ...