Leetcode每日一题:104.maximum-depth-of-binary-tree(二叉树的最大深度)
思路:这连续的几道题几乎都是用递归的思想,从结点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(二叉树的最大深度)相关推荐
- leetcode python3 简单题104. Maximum Depth of Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...
- 104. Maximum Depth of Binary Tree 二叉树的最大深度
给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...
- 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
- 【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 ...
- [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 ...
- leetcode python3 简单题111. Minimum Depth of Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十一题 (1)题目 英文: Given a binary tree, fin ...
最新文章
- YourEclipse—不只是Eclipse开发者社区
- java web应用解决乱码问题
- KV数据存储:持久化
- native关键字(本地方法)、 java调用so动态链接库
- 移动相关HTML设置
- ibatis插入正确但查询不出数据的问题
- jmap之使用说明与JVM配置
- c盘users的用户名怎么改_做完这几个操作,我从C盘中清理了30G垃圾文件
- linux家用系统版本,查看linux系统版本
- Reactor与Netty基本操作流程总结
- win7安装.Net Framework 4,出现错误码(1603,0x80070643)
- Echarts 3d地球toolstips实现
- composer.lock(锁文件)到底要不要提交到项目的版本库中?
- IPSec ×××实验
- 用dango框架搭建博客网站
- vue实现盒子的拖拽移动
- A Game of Thrones(84)
- 【Linux】【开发环境】【RHEL】开发环境搭建系列之十——Linux主机环境下挂载samba服务器
- 车间动态调度的研究方法
- 一筐鸡蛋一个一个拿正好拿完java_求解:一筐鸡蛋:9个9个拿正好拿完8个8个拿还剩1个...