Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 681 Bytes

balanced_binarytree.md

File metadata and controls

27 lines (22 loc) · 681 Bytes

110. Balanced Binary Tree

Code
class Solution {
public:
    bool isBalanced(TreeNode* root) {
      bool ans = true;
      auto balance = [&](const auto& self, const auto& root) -> int {
        if (root == nullptr) return 0;
        
        int left = self(self, root -> left);
        int right = self(self, root -> right);
        
        ans = ans & (abs(left - right) <= 1);
        return max(left, right) + 1;
      };
      balance(balance, root);
      return ans;
    }
};