-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path110.平衡二叉树.cpp
41 lines (36 loc) · 950 Bytes
/
110.平衡二叉树.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* @lc app=leetcode.cn id=110 lang=cpp
*
* [110] 平衡二叉树
*/
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
// @lc code=start
class Solution
{
public:
bool isBalanced(TreeNode *root)
{
if (!root)
return true;
bool temp = (get_height(root->left) - get_height(root->right) < 2) && (get_height(root->left) - get_height(root->right) > -2);
return temp && isBalanced(root->left) && isBalanced(root->right);
}
int get_height(TreeNode *node)
{
if (!node)
return -1;
return max(get_height(node->left), get_height(node->right)) + 1;
}
};
// @lc code=end