s0110
This commit is contained in:
parent
e5b2a0bd94
commit
495a134494
15
include/s0110_balanced_binary_tree.hpp
Normal file
15
include/s0110_balanced_binary_tree.hpp
Normal 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
|
17
src/s0110_balanced_binary_tree.cpp
Normal file
17
src/s0110_balanced_binary_tree.cpp
Normal 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; }
|
Loading…
Reference in New Issue
Block a user