解法一:递归

 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. zookeeper 监听事件 PathChildrenCacheListener

    zookeeper 监听事件 PathChildrenCacheListener PathChildrenCacheListener一次父节点注册,监听每次子节点操作,不监听自身和查询. 1.测试类: ...

  2. linux下不同服务器间数据传输(wget,scp)

    一.wget是Linux下最常用的http/ftp文件下载工具1.wget断点续传,只需要加上-c参数即可,例如:代码:wget-chttp://www.abc.com/abc.zip-Oabc.zi ...

  3. php AES-128-CBC 加密 通信java

    <?phpheader("Content-type: text/html; charset=utf-8");require('MyAES.php');class Xfb{ / ...

  4. git失败案例

    哈哈哈,git终于能push了,哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈 我怀疑是系统版本的问题,之前一直不没能pu ...

  5. # 20145314《信息安全系统设计基础》期中复习总结 Part B

    20145314<信息安全系统设计基础>期中复习总结 Part B 学习知识点内容总结 复习线索:http://group.cnblogs.com/topic/73069.html 本周的 ...

  6. Linux命令awk

    1.简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大. 简单来说awk就是把文件逐行读入,默认以空格为分隔符将每行切片,切开的部 ...

  7. RedHat5.4 使用 centOS5源更新

    1.卸载HedHat5.4的yum命令     先查看RedHat上是否安装yum            删除所有的yum软件     rpm -qa | grep yum | xargs rpm - ...

  8. ubuntu16.04下安装mysql详细步骤

    1.如果要搭建服务器先去购买一个云主机,比如阿里云.京东云.新网等等都有卖.这里推荐使用京东云服务器,因为最近在搞活动.一元可体验两个月(可能现在活动已经过了,但在京东云里有免费领一个月的,学生机也有 ...

  9. Student : IComparable<Student> 以及逆变和协变

    IComparable<Student>是Student的父类,所以IComparable<Student>可以接收Student.但是在使用CompareTo方法的时候,必须 ...

  10. Sublime使用记录之SublimeServer插件介绍

    Sublime使用记录之SublimeServer插件介绍 介绍:sublimeServer