-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy path39.py
33 lines (28 loc) · 782 Bytes
/
39.py
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
30
31
32
33
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def TreeDepth(self, pRoot):
# write code here
if pRoot is None:
return 0
deepth1 = self.TreeDepth(pRoot.left)
if deepth1==-1:
return -1
deepth2 = self.TreeDepth(pRoot.right)
if deepth2==-1:
return -1
if abs(deepth1 - deepth2) > 1:
return -1
return max(1 + deepth1, 1 + deepth2)
def IsBalanced_Solution(self, pRoot):
# write code here
if pRoot is None:
return True
deepth1 = self.TreeDepth(pRoot)
if deepth1 == -1:
return False
return True