File tree Expand file tree Collapse file tree 1 file changed +18
-8
lines changed Expand file tree Collapse file tree 1 file changed +18
-8
lines changed Original file line number Diff line number Diff line change 7
7
class Solution :
8
8
def isBalanced (self , root : Optional [TreeNode ]) -> bool :
9
9
10
- def dfs (node ):
10
+ def helper (node ):
11
11
if not node :
12
12
return [True , 0 ]
13
13
14
- left = dfs (node .left )
15
- right = dfs (node .right )
16
- cal = left [0 ] and right [0 ] and abs (left [1 ]- right [1 ]) < 2
17
- if call == False :
18
- return [False , 0 ]
14
+ left = helper (node .left )
15
+ right = helper (node .right )
16
+
17
+ # if not left[0] or not right[0]:
18
+ # return [False, None]
19
+
20
+ is_unbalanced = left [0 ] and right [0 ] and abs (left [1 ] - right [1 ]) < 2
21
+ if not is_unbalanced :
22
+ return [False , None ]
19
23
else :
20
- return [cal , max (left [1 ],right [1 ]) + 1 ]
21
- return dfs (root )[0 ]
24
+
25
+ return [is_unbalanced , max (left [1 ], right [1 ]) + 1 ]
26
+
27
+ return helper (root )[0 ]
28
+
29
+
30
+
31
+
22
32
You can’t perform that action at this time.
0 commit comments