思路:这连续的几道题几乎都是用递归的思想,从结点root开始,我就设定一个count和一个max,
左孩子不空,就root->left,count++,继续遍历,右孩子不空,就root->right,count++继续遍历,如果遍历到某个叶子节点时(左右孩子都为空),就判断count与max,看是否需要更新max的值;

/*** Definition for a binary tree node.* 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;int result = 0;maxTreeDepth(root, 1, result);return result;}void maxTreeDepth(TreeNode* root, int count, int &max){if (root->left == NULL && root->right == NULL) //叶子结点,判断是否更新max值{if (count > max)max = count;return;}if (root->left)//左端不空,就往左端继续遍历{maxTreeDepth(root->left, count+1, max);}if (root->right != NULL)//右端不空,就往右端继续遍历{maxTreeDepth(root->right, count+1, max);}}
};

Leetcode每日一题:104.maximum-depth-of-binary-tree(二叉树的最大深度)相关推荐

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

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

  2. 104. Maximum Depth of Binary Tree 二叉树的最大深度

    给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...

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

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

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

  5. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

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

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

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

  10. leetcode python3 简单题111. Minimum Depth of Binary Tree

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

最新文章

  1. YourEclipse—不只是Eclipse开发者社区
  2. java web应用解决乱码问题
  3. KV数据存储:持久化
  4. native关键字(本地方法)、 java调用so动态链接库
  5. 移动相关HTML设置
  6. ibatis插入正确但查询不出数据的问题
  7. jmap之使用说明与JVM配置
  8. c盘users的用户名怎么改_做完这几个操作,我从C盘中清理了30G垃圾文件
  9. linux家用系统版本,查看linux系统版本
  10. Reactor与Netty基本操作流程总结
  11. win7安装.Net Framework 4,出现错误码(1603,0x80070643)
  12. Echarts 3d地球toolstips实现
  13. composer.lock(锁文件)到底要不要提交到项目的版本库中?
  14. IPSec ×××实验
  15. 用dango框架搭建博客网站
  16. vue实现盒子的拖拽移动
  17. A Game of Thrones(84)
  18. 【Linux】【开发环境】【RHEL】开发环境搭建系列之十——Linux主机环境下挂载samba服务器
  19. 车间动态调度的研究方法
  20. 一筐鸡蛋一个一个拿正好拿完java_求解:一筐鸡蛋:9个9个拿正好拿完8个8个拿还剩1个...

热门文章

  1. 04-----赋值运算符
  2. ThreadLocal类的实现用法
  3. swift-switch使用方法
  4. Spring MVC http消息转换类
  5. .net random伪随机数
  6. Office Open XML学习(1)-创建excel文档,并向单元格中插入字符串
  7. [转]Linux TCP/IP 协议栈的关键数据结构Socket Buffer(sk_buff )
  8. 前端框架之Vue(1)-第一个Vue实例
  9. angular2 图表指令ng2-echarts
  10. mysql 常用命令