This commit is contained in:
Sainnhe Park 2023-01-31 11:19:09 +08:00
parent e5b2a0bd94
commit 495a134494
2 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,15 @@
#ifndef S0110_BALANCED_BINARY_TREE_HPP
#define S0110_BALANCED_BINARY_TREE_HPP
#include <algorithm>
#include "structures.hpp"
using namespace std;
class S0110 {
public:
bool isBalanced(TreeNode* root);
};
#endif

View File

@ -0,0 +1,17 @@
#include "s0110_balanced_binary_tree.hpp"
struct Info {
bool isBalanced;
int depth;
};
Info getInfo(TreeNode* root) {
if (root == nullptr) return Info{true, 0};
Info left = getInfo(root->left);
Info right = getInfo(root->right);
return Info{
left.isBalanced && right.isBalanced && abs(left.depth - right.depth) <= 1,
max(left.depth, right.depth) + 1};
}
bool S0110::isBalanced(TreeNode* root) { return getInfo(root).isBalanced; }