-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogle5.py
More file actions
60 lines (45 loc) · 1.73 KB
/
Google5.py
File metadata and controls
60 lines (45 loc) · 1.73 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 15 16:30:42 2019
@author: oscarbarbier
Good morning! Here's your coding interview problem for today.
This problem was asked by Google.
A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value.
Given the root to a binary tree, count the number of unival subtrees.
For example, the following tree has 6 unival subtrees:
0
/ \
1 0
/ \
1 0
/ \ \
1 1 0
"""
class Arbre() :
def __init__(self, value, left = None, right = None) :
self.value = value
self.left = left
self.right = right
def is_unival(Arbre) :
if Arbre.left == None and Arbre.right == None :
return True, Arbre.value
if Arbre.left == None :
return ((is_unival(Arbre.right)[0] and Arbre.value == is_unival(Arbre.right)[1]), Arbre.value)
if Arbre.right == None :
return ((is_unival(Arbre.left)[0] and Arbre.value == is_unival(Arbre.left)[1]), Arbre.value)
else :
return ((is_unival(Arbre.left)[0] and is_unival(Arbre.right)[0] and Arbre.value == is_unival(Arbre.left)[1] and Arbre.value == is_unival(Arbre.right)[1]), Arbre.value)
def is_u(Arbre) :
return is_unival(Arbre)[0]
def get_unival(Arbre) :
if Arbre == None :
return 0
if Arbre.left == None and Arbre.right == None :
return 1
else :
return(int(is_u(Arbre)) + get_unival(Arbre.left) + get_unival(Arbre.right))
if __name__ == "__main__" :
Tree = Arbre(0, left=Arbre(1), right=Arbre(0, left = Arbre(1, left = Arbre(1), right = Arbre(1)),right = Arbre(0, right = Arbre(0))))
unival = Arbre( 1, left = Arbre(1), right = Arbre(0) )
print(get_unival(Tree))