Skip to content

Commit dec96b0

Browse files
committed
Adding Simple Solution - Problem - 965 - Unival BT
1 parent fa69492 commit dec96b0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Unival_BT/SimpleSolution.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
##==================================
2+
## Leetcode May Challenge
3+
## Username: Vanditg
4+
## Year: 2020
5+
## Problem: 965
6+
## Problem Name: Univalued Binary Tree
7+
##===================================
8+
#
9+
#A binary tree is univalued if every node in the tree has the same value.
10+
#
11+
Return true if and only if the given tree is univalued.
12+
#
13+
#Example 1:
14+
#
15+
#Input: [1,1,1,1,1,null,1]
16+
#Output: true
17+
#Example 2:
18+
#
19+
#Input: [2,2,2,5,2]
20+
#Output: false
21+
class Solution:
22+
def isUnivalTree(self, root):
23+
if root is None: #Condition-check: If root is empty
24+
return True
25+
def inOrder(root): #Defining helper method - inOrder
26+
if root is None: #Condition-check: If root is empty
27+
return [] #We return empty list
28+
inLeft = inOrder(root.left) #Initialize inLeft, using recursion for left sub-tree
29+
rootVal = [root.val] #Initialize rootVal
30+
inRight = inOrder(root.right) #Initialize inRight, using recursion for right sub-tree
31+
tmp = inOrder(root) #Using helper method to create a list of our root
32+
return len(set(tmp)) == 1 #Return true or false based on the length of the list

0 commit comments

Comments
 (0)