leetcode/src/s0110_balanced_binary_tree.cpp

18 lines
457 B
C++

#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; }