This problem looks busy at first, but the accepted solution is built around one steady invariant. For 110. Balanced Binary Tree, the solution in this repository is mainly a two pointers solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are treeDepth, isSubtreeBalanced, isBalanced, dfsHeight.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 40 ms, faster than 5.25% of C++ online submissions for Balanced Binary Tree.
02//Memory Usage: 17.3 MB, less than 72.64% of C++ online submissions for Balanced Binary Tree.
03
04https://leetcode.com/problems/balanced-binary-tree/discuss/35691/The-bottom-up-O(N)-solution-would-be-better
05
06/**
07top-down approach
08time complexity:
09treeDepth needs O(n), and we need to calculate for every node, so total O(n^2)
10**/
11
12/**
13 * Definition for a binary tree node.
14 * struct TreeNode {
15 * int val;
16 * TreeNode *left;
17 * TreeNode *right;
18 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
19 * };
20 */
21class Solution {
22public:
23 int treeDepth(TreeNode* root){
24 if(root == NULL) return 0;
25 return 1 + max(treeDepth(root->left), treeDepth(root->right));
26 }
27 // bool isSubtreeBalanced(TreeNode* root){
28 // if(root == NULL) return true;
29 // return abs(treeDepth(root->left) - treeDepth(root->right)) <= 1;
30 // }
31 bool isBalanced(TreeNode* root) {
32 if(root == NULL) return true;
33 // return isSubtreeBalanced(root) && isSubtreeBalanced(root->left) && isSubtreeBalanced(root->right);
34 // cout << root->val << endl;
35 // if(abs(treeDepth(root->left) - treeDepth(root->right)) > 1){
36 // cout << "***" << root->val << "***" << endl;
37 // cout << (root->left->val) << " " <<(root->right->val) << endl;
38 // cout << treeDepth(root->left) << " " << treeDepth(root->right) << endl;
39 // }
40 return (abs(treeDepth(root->left) - treeDepth(root->right)) <= 1) && isBalanced(root->left) && isBalanced(root->right);
41 }
42};
43
44/**
45bottom-up approach
46time complexity: O(n)
47**/
48
49//Runtime: 16 ms, faster than 99.39% of C++ online submissions for Balanced Binary Tree.
50//Memory Usage: 17.1 MB, less than 88.68% of C++ online submissions for Balanced Binary Tree.
51
52/**
53class Solution {
54public:
55 int dfsHeight(TreeNode* root){
56 if(!root) return 0;
57 int left = dfsHeight(root->left);
58 if(left == -1) return -1;
59 int right = dfsHeight(root->right);
60 if(right == -1) return -1;
61 if(abs(left - right) > 1) return -1;
62 return max(left, right)+1;
63 }
64 bool isBalanced(TreeNode* root) {
65 return dfsHeight(root) != -1;
66 }
67};
68**/
Cost