当前位置:网站首页>[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 :
边栏推荐
- C语言:扫雷小游戏
- Binder initialization process
- Voice live broadcast system -- a necessary means to improve the security of cloud storage
- 【剑指offer】面试题51:数组中的逆序对——归并排序
- C语言:自定义类型
- Network principle (1) - overview of basic principles
- Go language learning notes (1)
- [tensorboard] oserror: [errno 22] invalid argument processing
- Huawei's general card identification function enables multiple card bindings with one key
- Hyperlink parsing in MD: parsing `this$ Set() `, ` $` should be preceded by a space or escape character`\`
猜你喜欢
![[daily question 1] 558. Intersection of quadtrees](/img/96/16ec3031161a2efdb4ac69b882a681.png)
[daily question 1] 558. Intersection of quadtrees

多线程带来的的风险——线程安全

Is low code the future of development? On low code platform

Insert sort directly

Troubleshooting the slow startup of spark local programs
![[sword finger offer] interview question 41: median in data flow - large and small heap implementation](/img/c3/7caf008b3bd4d32a00b74f2c508c65.png)
[sword finger offer] interview question 41: median in data flow - large and small heap implementation

Causes and solutions of deadlock in threads

【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN

Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers

网络原理(2)——网络开发
随机推荐
Multimap case
Zhaoqi scientific innovation and entrepreneurship competition planning and undertaking organization, mass entrepreneurship and innovation platform, project landing and docking
Summer Challenge harmonyos realizes a hand-painted board
go语言慢速入门——基本内置类型
C:什么是函数中的返回值(转)
数据表的约束以及设计、联合查询——8千字攻略+题目练习解答
Analysis of spark task scheduling exceptions
【剑指offer】面试题41:数据流中的中位数——大、小堆实现
Go language slow start - package
Troubleshooting the slow startup of spark local programs
C language: Sanzi game
网络层的IP协议
[regular expression] single character matching
C language: string function and memory function
C语言:三子棋游戏
C语言:动态内存函数
leetcode-1:两数之和
Is the array name the address of the first element?
多线程带来的的风险——线程安全
JS uses extension operators (...) to simplify code and simplify array merging