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.

与Minimum Depth of Binary Tree对照看

解法一:递归,子树高度+1。

/*** Definition for binary tree* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int maxDepth(TreeNode *root) {if(root == NULL)return 0;elsereturn max(maxDepth(root->left), maxDepth(root->right)) + 1;}
};

解法二:深度优先遍历,栈的最大容量即最大深度

/*** Definition for binary tree* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int maxDepth(TreeNode *root) {if(root == NULL)return 0;stack<TreeNode*> stk;unordered_map<TreeNode*, bool> visited;stk.push(root);int ret = 1;visited[root] = true;while(!stk.empty()){TreeNode* top = stk.top();if(top->left && visited[top->left] == false){stk.push(top->left);ret = max(ret, (int)stk.size());visited[top->left] = true;continue;}if(top->right && visited[top->right] == false){stk.push(top->right);ret = max(ret, (int)stk.size());visited[top->right] = true;continue;}stk.pop();}return ret;}
};

【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)相关推荐

  1. 【leetcode】104. Maximum Depth of Binary Tree

    1. 题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...

  2. 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...

  3. LC 104. Maximum Depth of Binary Tree

    1.题意 104. Maximum Depth of Binary Tree Easy 98540 Given a binary tree, find its maximum depth. The m ...

  4. 【LeetCode从零单排】No104 Maximum Depth of Binary Tree

    题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

  5. 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 l ...

  6. leetcode python3 简单题104. Maximum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...

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

  8. [swift] 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 ...

  9. Leet Code OJ 104. Maximum Depth of Binary Tree [Difficulty: Easy]

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

最新文章

  1. 多维分析中的 UV 与 PV
  2. 会计期间在ERP开发中的设计思路
  3. 0基础搭建Hadoop大数据处理-初识
  4. 手把手教你如何写简历
  5. Visual Studio 20周年软件趋势随想
  6. 前端学习(2154):webpack横幅plugin的使用
  7. diskgeniusv4.4.0_一文上手最新Tensorflow2.0系列|TensorFlow2.0安装
  8. html5shiv主要解决IE6-8 无法识别HTML5的新标签,父节点不能包裹子元素,以及应用CSS样式...
  9. html中加一个框与底部平齐,div+CSS实现单选复选框与文本对齐
  10. 对服务器端接口的调用,自己手写了一个脚本,但返回信息的中文总是乱码(这个方法很不错,重要的是解决思路,寻找手写脚本与录制脚本 生成目录文件的区别)...
  11. 沉寂了多年金价为何会连续上涨 专家指出真正原因
  12. Linux中ctrl+z 和trl+c的区别以及jobs、bg、fg命令
  13. 黑马程序员----------Java新特性反射 泛型
  14. 【战术性mark】JS 复制内容到剪贴板
  15. 【图形学】计算机图形学-练习题5
  16. html实现圆形计时器特效,如何用css3实现圆形倒计时
  17. 8大底层逻辑,提升思维能力
  18. 河南科技学院教务管理系统服务器,河南科技学院教务管理系统http://jwgl.hist.edu.cn/jwweb/...
  19. 教师是唯一无法被人工智能取代的职业
  20. 论文复刻:排污权机制是否提高了企业全要素生产率(附代码、数据、原文献)

热门文章

  1. close 不弹出对话框
  2. PowerDesigner 使用的一些技巧
  3. android11beta支持什么手机,Android 11 Beta1发布,新增多种功能,网友:Android基于 Flyme...
  4. MySQL分组查询—添加分组后筛选
  5. 什么是泛型、为什么要使用以及泛型擦除
  6. 并发的发展历史-线程的出现
  7. 往IOC 容器中添加组件的方式
  8. xml方式实现aop-切点表达式的抽取
  9. springboot日志配置
  10. 循环控制_continue语句