当前位置:网站首页>[sword finger offer] interview question 55 - Ⅰ / Ⅱ: depth of binary tree / balanced binary tree
[sword finger offer] interview question 55 - Ⅰ / Ⅱ: depth of binary tree / balanced binary tree
2022-07-27 15:52:00 【Jocelin47】
The finger of the sword Offer 55 - I. The depth of the binary tree
Method 1 :
/** * 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;
// }
};
The finger of the sword Offer 55 - II. Balanced binary trees
Method 1 : recursive
/** * 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;
// }
};
Method 2 :
边栏推荐
- On juicefs
- C语言:字符串函数与内存函数
- Interview focus - TCP protocol of transport layer
- Explanation of various attributes of "router link"
- Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
- 网络设备硬核技术内幕 路由器篇 22
- Go language learning notes (1)
- C:浅谈函数
- 实体类(VO,DO,DTO)的划分
- C语言:自定义类型
猜你喜欢

MySQL表数据的增删查改

Spark troubleshooting finishing

Spark Bucket Table Join

Voice live broadcast system -- a necessary means to improve the security of cloud storage

【剑指offer】面试题50:第一个只出现一次的字符——哈希表查找

leetcode25题:K 个一组翻转链表——链表困难题目详解

C language: function stack frame
![[tensorboard] oserror: [errno 22] invalid argument processing](/img/bf/c995f487607e3b307a268779ec1e94.png)
[tensorboard] oserror: [errno 22] invalid argument processing

Spark 3.0 DPP implementation logic

C language: string function and memory function
随机推荐
设置提示框位置随鼠标移动,并解决提示框显示不全的问题
Analysis of spark task scheduling exceptions
[sword finger offer] interview question 41: median in data flow - large and small heap implementation
MLX90640 红外热成像仪测温传感器模块开发笔记(七)
After the table is inserted into an in row formula, the cell loses focus
C:什么是函数中的返回值(转)
[Yunxiang book club issue 13] common methods of viewing media information and processing audio and video files in ffmpeg
MySQL表数据的增删查改
Learn parquet file format
Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
C语言:函数栈帧
Database: use the where statement to retrieve (header song)
C language: string function and memory function
Binder初始化过程
Half find
CAS compares the knowledge exchanged, ABA problems, and the process of lock upgrading
Spark RPC
折半插入排序
【剑指offer】面试题41:数据流中的中位数——大、小堆实现
Using Prometheus to monitor spark tasks