当前位置:网站首页>【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
2022-07-27 14:24:00 【Jocelin47】
剑指 Offer 55 - I. 二叉树的深度
方法一:
/** * 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 == nullptr )
return 0;
return max( maxDepth(root->left), maxDepth(root->right)) + 1;
}
// int maxDepth(TreeNode* root)
// {
// if( root == nullptr )
// return 0;
// int lefHeight = maxDepth(root->left);
// int rightHeight = maxDepth(root->right);
// return (lefHeight > rightHeight) ? lefHeight + 1 : rightHeight + 1;
// }
};
剑指 Offer 55 - II. 平衡二叉树
方法一:递归
/** * 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:
bool isBalanced(TreeNode* root) {
if( root == nullptr )
return true;
if( abs( Depth(root->left) - Depth(root->right) ) > 1 )
return false;
return isBalanced(root->left) && isBalanced(root->right);
}
int Depth(TreeNode* root) {
if(root == nullptr )
return 0;
return max( Depth(root->left), Depth(root->right)) + 1;
}
// int Depth(TreeNode* root)
// {
// if( root == nullptr )
// return 0;
// int lefHeight = Depth(root->left);
// int rightHeight = Depth(root->right);
// return (lefHeight > rightHeight) ? lefHeight + 1 : rightHeight + 1;
// }
};
方法二:
边栏推荐
- 初探JuiceFS
- Leetcode 781. rabbit hash table in forest / mathematical problem medium
- Pytorch replaces some components in numpy. / / please indicate the source of the reprint
- Spark 本地程序启动缓慢问题排查
- Principle of MOS tube to prevent reverse connection of power supply
- Multi table query_ Exercise 1 & Exercise 2 & Exercise 3
- Leetcode 783. binary search tree node minimum distance tree /easy
- Photoelectric isolation circuit design scheme (six photoelectric isolation circuit diagrams based on optocoupler and ad210an)
- Unity 鼠标控制第一人称摄像机视角
- Spark RPC
猜你喜欢

Record record record

Spark Bucket Table Join

Watermelon book machine learning reading notes Chapter 1 Introduction

3.3-5v conversion

Leetcode interview question 17.21. water volume double pointer of histogram, monotonic stack /hard

HaoChen CAD building 2022 software installation package download and installation tutorial

Unity性能优化------渲染优化(GPU)之LOD(Level of detail)

After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues

Spark TroubleShooting整理

西瓜书《机器学习》阅读笔记之第一章绪论
随机推荐
Lua study notes
Spark 3.0 DPP实现逻辑
Leetcode 783. binary search tree node minimum distance tree /easy
实现自定义Spark优化规则
Network equipment hard core technology insider router Chapter 18 dpdk and its prequel (III)
Selenium reports an error: session not created: this version of chromedriver only supports chrome version 81
HJ8 合并表记录
Basic usage of kotlin
Unity性能优化------渲染优化(GPU)之Occlusion culling(遮挡剔除)
Spark3中Catalog组件设计和自定义扩展Catalog实现
Network equipment hard core technology insider router Chapter 3 Jia Baoyu sleepwalking in Taixu Fantasy (middle)
Multi table query_ Exercise 1 & Exercise 2 & Exercise 3
扩展Log4j支持日志文件根据时间分割文件和过期文件自动删除功能
Cap theory and base theory
适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制
Watermelon book machine learning reading notes Chapter 1 Introduction
The first common node of the two linked lists of "Jianzhi offer"
How to package AssetBundle
js运用扩展操作符(…)简化代码,简化数组合并
华云数据打造完善的信创人才培养体系 助力信创产业高质量发展