解法一:递归

 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. netbeans通过wsdl生成webservice的UTF8问题

    在netbeans通过wsdl方式生成的webservice,打开类文件时,提示无法通过UTF-8打开. 这是因为默认生成的文件不是UTF-8格式的,解决方案如下: 1.打开netbeans的安装目录 ...

  2. pyinstaller 打包生成的exe文件,在其他电脑上报错

    解决方法: 1.第一种情况,在打包的时候不要加参数-w,看一下执行exe文件后出现的报错再看下一步的行动 2.应该是需要装一个VC 2015 x64(下载地址:https://www.microsof ...

  3. 浅析SQL注入

    body, td { font-family: calibri; font-size: 10pt; } 演示 记得以前瞎鼓捣的时候,学过一个传说中的SQL注入万能字符串,是这个样子的' or '1'= ...

  4. 20135320赵瀚青LINUX内核分析第三周学习笔记

    赵瀚青原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 概述 本周是学习的主要是构造 ...

  5. 当新手使用JS库遇到问题怎么办

    见标题,知其意.在做网站时候,其实我们会用很多JS库,网络上流行的和公司自己封装的,这些东西都很好用,但是或多或少的有些bug或者有一些缺陷,即使真的很完善,但也可能达不到自己特定的一些需求.所以遇到 ...

  6. Sqoop相关

    # sqoop安装 1. 常规步骤(安装在一台节点上即可) 由于sqoop2配置相对比较麻烦,此次使用的是sqoop1进行演示. 上传sqoop-1.4.4.bin_hadoop-2.0.4-alph ...

  7. 一文弄懂神经网络中的反向传播法——BackPropagation【转】

    本文转载自:https://www.cnblogs.com/charlotte77/p/5629865.html 一文弄懂神经网络中的反向传播法——BackPropagation   最近在看深度学习 ...

  8. 分词工具比较及使用(ansj、hanlp、jieba)

    一.分词工具 ansj.hanlp.jieba 二.优缺点 1.ansj 优点: 提供多种分词方式 可直接根据内部词库分出人名.机构等信息 可构造多个词库,在分词时可动态选择所要使用的词库缺点: 自定 ...

  9. SpringMVC两种处理器适配器

    1.实现Controller接口的处理器适配器 package com.xiaostudy; import javax.servlet.http.HttpServletRequest; import ...

  10. MATLAB 图形图像处理

    theme: MATLAB author: pprp date: 2018/2/2 --- MATLAB 图形图像处理 二维绘图命令 plot 线性空间 plot(t,[x1,x2,x3]) : 在同 ...