File tree 4 files changed +95
-0
lines changed
algorithms/BalancedBinaryTree
4 files changed +95
-0
lines changed Original file line number Diff line number Diff line change 30
30
+ [ 100 Same Tree] ( algorithms/SameTree )
31
31
+ [ 101 Symmetric Tree] ( algorithms/SymmetricTree )
32
32
+ [ 104 Maximum Depth of Binary Tree] ( algorithms/MaximumDepthofBinaryTree )
33
+ + [ 110 Balanced Binary Tree] ( algorithms/BalancedBinaryTree )
33
34
+ [ 111 Minimum Depth of Binary Tree] ( algorithms/MinimumDepthofBinaryTree )
34
35
+ [ 112 Path Sum(前序遍历)] ( algorithms/PathSum )
35
36
+ [ 113 Path Sum II(前序遍历)] ( algorithms/PathSum2 )
Original file line number Diff line number Diff line change
1
+ #include <cstdlib>
2
+ using namespace std;
3
+ struct TreeNode {
4
+ int val;
5
+ TreeNode *left;
6
+ TreeNode *right;
7
+ TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8
+ };
9
+ class Solution {
10
+ public:
11
+ bool isBalanced(TreeNode *root) {
12
+ if (root == NULL)
13
+ return true;
14
+ if (root->left && root->right)
15
+ return isBalanced(root->left) && isBalanced(root->right);
16
+ if (root->left) {
17
+ return root->left->left && !root->left->right;
18
+ if (root->left->left || root->left->right)
19
+ return false;
20
+ else
21
+ return true;
22
+ }
23
+ }
24
+
25
+ };
26
+ int main(int argc, char **argv)
27
+ {
28
+ return 0;
29
+ }
Original file line number Diff line number Diff line change
1
+ ## Balanced Binary Tree
2
+
3
+ Given a binary tree, determine if it is height-balanced.
4
+
5
+ For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
6
+
7
+ ## Solution
8
+
9
+ 分别求左右子树的高度,如果高度之差大于1,则不是平衡树
10
+
11
+ 而如果子树不是平衡树,结果也一定不是平衡树,因此再求左右子树的同时,顺便判断是否平衡树,能够提高速度
12
+
13
+ ## Code
14
+ ``` cpp
15
+ bool isBalanced (TreeNode * root) {
16
+ bool unbalenced = false;
17
+ getHeight(unbalenced, root);
18
+ return !unbalenced;
19
+ }
20
+ int getHeight(bool &unbalenced, TreeNode * root) {
21
+ if (root == NULL || unbalenced)
22
+ return -1;
23
+ int left = getHeight(unbalenced, root->left);
24
+ int right = getHeight(unbalenced, root->right);
25
+ if (abs(left, right) > 1) {
26
+ unbalenced = true;
27
+ }
28
+ return 1 + (left > right ? left : right);
29
+ }
30
+ ```
Original file line number Diff line number Diff line change
1
+ #include < cstdlib>
2
+ using namespace std ;
3
+ struct TreeNode {
4
+ int val;
5
+ TreeNode *left;
6
+ TreeNode *right;
7
+ TreeNode (int x) : val(x), left(NULL ), right(NULL ) {}
8
+ };
9
+ static inline int abs (int a, int b)
10
+ {
11
+ return a > b ? a - b : b - a;
12
+ }
13
+ class Solution {
14
+ public:
15
+ bool isBalanced (TreeNode *root) {
16
+ bool unbalenced = false ;
17
+ getHeight (unbalenced, root);
18
+ return !unbalenced;
19
+ }
20
+ int getHeight (bool &unbalenced, TreeNode *root) {
21
+ if (root == NULL || unbalenced)
22
+ return -1 ;
23
+ int left = getHeight (unbalenced, root->left );
24
+ int right = getHeight (unbalenced, root->right );
25
+ if (abs (left, right) > 1 ) {
26
+ unbalenced = true ;
27
+ }
28
+ return 1 + (left > right ? left : right);
29
+ }
30
+
31
+ };
32
+ int main (int argc, char **argv)
33
+ {
34
+ return 0 ;
35
+ }
You can’t perform that action at this time.
0 commit comments