|
| 1 | +#User function Template for python3 |
| 2 | + |
| 3 | + |
| 4 | +'''class Node: |
| 5 | +# Constructor to create a new Node |
| 6 | +def __init__(self, data): |
| 7 | +self.data = data |
| 8 | +self.left = None |
| 9 | +self.right = None''' |
| 10 | + |
| 11 | + |
| 12 | +#Function to check whether a binary tree is balanced or not. |
| 13 | +class Solution: |
| 14 | +def isBalanced(self,root): |
| 15 | +def find_h(root): |
| 16 | +if not root: |
| 17 | +return 0 |
| 18 | + |
| 19 | +l=find_h(root.left) |
| 20 | +r=find_h(root.right) |
| 21 | +if l<0 or r<0: |
| 22 | +return -1 |
| 23 | +if abs(l-r)>1: |
| 24 | +return -1 |
| 25 | +else: |
| 26 | +return 1+max(l,r) |
| 27 | +if find_h(root)==-1: |
| 28 | +return False |
| 29 | +return True |
| 30 | + |
| 31 | +#add code here |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +#{ |
| 36 | +# Driver Code Starts |
| 37 | +#Initial Template for Python 3 |
| 38 | + |
| 39 | +from collections import deque |
| 40 | +# Tree Node |
| 41 | +class Node: |
| 42 | +def __init__(self, val): |
| 43 | +self.right = None |
| 44 | +self.data = val |
| 45 | +self.left = None |
| 46 | + |
| 47 | +# Function to Build Tree |
| 48 | +def buildTree(s): |
| 49 | +#Corner Case |
| 50 | +if(len(s)==0 or s[0]=="N"): |
| 51 | +return None |
| 52 | + |
| 53 | +# Creating list of strings from input |
| 54 | +# string after spliting by space |
| 55 | +ip=list(map(str,s.split())) |
| 56 | + |
| 57 | +# Create the root of the tree |
| 58 | +root=Node(int(ip[0])) |
| 59 | +size=0 |
| 60 | +q=deque() |
| 61 | + |
| 62 | +# Push the root to the queue |
| 63 | +q.append(root) |
| 64 | +size=size+1 |
| 65 | + |
| 66 | +# Starting from the second element |
| 67 | +i=1 |
| 68 | +while(size>0 and i<len(ip)): |
| 69 | +# Get and remove the front of the queue |
| 70 | +currNode=q[0] |
| 71 | +q.popleft() |
| 72 | +size=size-1 |
| 73 | + |
| 74 | +# Get the current node's value from the string |
| 75 | +currVal=ip[i] |
| 76 | + |
| 77 | +# If the left child is not null |
| 78 | +if(currVal!="N"): |
| 79 | + |
| 80 | +# Create the left child for the current node |
| 81 | +currNode.left=Node(int(currVal)) |
| 82 | + |
| 83 | +# Push it to the queue |
| 84 | +q.append(currNode.left) |
| 85 | +size=size+1 |
| 86 | +# For the right child |
| 87 | +i=i+1 |
| 88 | +if(i>=len(ip)): |
| 89 | +break |
| 90 | +currVal=ip[i] |
| 91 | + |
| 92 | +# If the right child is not null |
| 93 | +if(currVal!="N"): |
| 94 | + |
| 95 | +# Create the right child for the current node |
| 96 | +currNode.right=Node(int(currVal)) |
| 97 | + |
| 98 | +# Push it to the queue |
| 99 | +q.append(currNode.right) |
| 100 | +size=size+1 |
| 101 | +i=i+1 |
| 102 | +return root |
| 103 | + |
| 104 | + |
| 105 | +if __name__=="__main__": |
| 106 | +t=int(input()) |
| 107 | +for _ in range(0,t): |
| 108 | +s=input() |
| 109 | +root=buildTree(s) |
| 110 | +ob = Solution() |
| 111 | +if ob.isBalanced(root): |
| 112 | +print(1) |
| 113 | +else: |
| 114 | +print(0) |
| 115 | +# } Driver Code Ends |
0 commit comments