-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path110.cpp
More file actions
29 lines (28 loc) · 777 Bytes
/
110.cpp
File metadata and controls
29 lines (28 loc) · 777 Bytes
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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (Leetcode) 110
// Title: Balanced Binary Tree
// Link: https://leetcode.com/problems/balanced-binary-tree
// Idea: Recursion.
// Difficulty: easy
// Tags: binary-tree
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int height(TreeNode* node) {
if (node == nullptr) return 0;
return 1 + max(height(node->left), height(node->right));
}
bool isBalanced(TreeNode* root) {
if (root == nullptr) return true;
return abs(height(root->left) - height(root->right)) <= 1 &&
isBalanced(root->left) && isBalanced(root->right);
}
};