解法一:递归

 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的更多相关文章

  1. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

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

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

  4. 【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 ...

  5. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

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

  7. 【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 ...

  8. 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 ...

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

随机推荐

  1. 为什么修改Host不生效

    开发验证的好好的功能,提测后经常有测试反应功能有bug.很多原因都是测试切换host没生效造成的,为什么切换host后刷新页面了也没生效呢? 不生效原因: Keep-Alive 服务器在响应头设置了 ...

  2. JavaScript常用算法

    一.排序算法 1.Array.sort(function)(JavaScript原生排序算法)参数:比较函数(可选)若无参数,则按照首字母的ASCII码排序,比较函数的作用为确定排序 function ...

  3. Apache httpd服务部署

    1. yum安装 yum install httpd yum install httpd-devel yum install httpd-manual 2. 配置 vim /etc/httpd/con ...

  4. 20145327 《Java程序设计》第五周学习总结

    20145327 <Java程序设计>第五周学习总结 教材学习内容总结 try...catch:与C语言中程序流程和错误处理混在一起不同,Java中把正常流程放try块中,错误(异常)处理 ...

  5. mybatis关联配置(一对多配置)

    敲代码也有不少日子了,今天碰到个需求,就是定时器生成一张表,但是这个表的某些数据是从另外两张表中拿到的,定外两张表又是一对多的关系,想着咋在一个接口就能敲出来,大概结构如下 然后需要a表的数据(比如张 ...

  6. Java搞笑注释(佛-)

    // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // O\ = /O // ____/`---'\____ // . ' \\| ...

  7. React绑定this的三种方式

    React可以使用React.createClass.ES6 classes.纯函数3种方式构建组件.使用React.createClass会自动绑定每个方法的this到当前组件,但使用ES6 cla ...

  8. POJ_2182 Lost Cows(线段树的简单应用)

    基本思路就是,从后往前读取数字small[i].在剩余编号集合里(一开始剩余编号集合为全集)查找第small[i]+1个编号,该编号就是对应位置牛的编号. 若直接用数组来做,则每次查找都需要遍历前n个 ...

  9. MySQL MERGE存储引擎 简介及用法

    MERGE存储引擎把一组MyISAM数据表当做一个逻辑单元来对待,让我们可以同时对他们进行查询.构成一个MERGE数据表结构的各成员MyISAM数据表必须具有完全一样的结构.每一个成员数据表的数据列必 ...

  10. 【网络优化】Batch Normalization(inception V2) 论文解析(转)

    前言 懒癌翻了,这篇不想写overview了,公式也比较多,今天有(zhao)点(jie)累(kou),不想一点点写latex啦,读论文的时候感觉文章不错,虽然看似很多数学公式,其实都是比较基础的公式 ...