File tree 3 files changed +79
-0
lines changed
solution/0100-0199/0110.Balanced Binary Tree
3 files changed +79
-0
lines changed Original file line number Diff line number Diff line change @@ -116,6 +116,34 @@ class Solution {
116
116
}
117
117
```
118
118
119
+ ### ** JavaScript**
120
+
121
+ ``` js
122
+ /**
123
+ * Definition for a binary tree node.
124
+ * function TreeNode(val, left, right) {
125
+ * this.val = (val===undefined ? 0 : val)
126
+ * this.left = (left===undefined ? null : left)
127
+ * this.right = (right===undefined ? null : right)
128
+ * }
129
+ */
130
+ /**
131
+ * @param {TreeNode} root
132
+ * @return {boolean}
133
+ */
134
+ var isBalanced = function (root ) {
135
+ if (root == null ) return true ;
136
+ let left = root .left ;
137
+ let right = root .right ;
138
+ return isBalanced (left) && isBalanced (right) && Math .abs (depth (left) - depth (right)) <= 1 ;
139
+ };
140
+
141
+ function depth (root ) {
142
+ if (root == null ) return 0 ;
143
+ return Math .max (depth (root .left ), depth (root .right )) + 1 ;
144
+ }
145
+ ```
146
+
119
147
### ** C++**
120
148
121
149
``` cpp
Original file line number Diff line number Diff line change @@ -106,6 +106,34 @@ class Solution {
106
106
}
107
107
```
108
108
109
+ ### ** JavaScript**
110
+
111
+ ``` js
112
+ /**
113
+ * Definition for a binary tree node.
114
+ * function TreeNode(val, left, right) {
115
+ * this.val = (val===undefined ? 0 : val)
116
+ * this.left = (left===undefined ? null : left)
117
+ * this.right = (right===undefined ? null : right)
118
+ * }
119
+ */
120
+ /**
121
+ * @param {TreeNode} root
122
+ * @return {boolean}
123
+ */
124
+ var isBalanced = function (root ) {
125
+ if (root == null ) return true ;
126
+ let left = root .left ;
127
+ let right = root .right ;
128
+ return isBalanced (left) && isBalanced (right) && Math .abs (depth (left) - depth (right)) <= 1 ;
129
+ };
130
+
131
+ function depth (root ) {
132
+ if (root == null ) return 0 ;
133
+ return Math .max (depth (root .left ), depth (root .right )) + 1 ;
134
+ }
135
+ ```
136
+
109
137
### ** C++**
110
138
111
139
``` cpp
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val, left, right) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.left = (left===undefined ? null : left)
6
+ * this.right = (right===undefined ? null : right)
7
+ * }
8
+ */
9
+ /**
10
+ * @param {TreeNode } root
11
+ * @return {boolean }
12
+ */
13
+ var isBalanced = function ( root ) {
14
+ if ( root == null ) return true ;
15
+ let left = root . left ;
16
+ let right = root . right ;
17
+ return isBalanced ( left ) && isBalanced ( right ) && Math . abs ( depth ( left ) - depth ( right ) ) <= 1 ;
18
+ } ;
19
+
20
+ function depth ( root ) {
21
+ if ( root == null ) return 0 ;
22
+ return Math . max ( depth ( root . left ) , depth ( root . right ) ) + 1 ;
23
+ }
You can’t perform that action at this time.
0 commit comments