-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30 Days Of Code: Solutions.go
1082 lines (905 loc) · 24.6 KB
/
30 Days Of Code: Solutions.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************/
Day 0: Hello, World
package main
import ("fmt"
"bufio"
"os"
)
func main() {
//Enter your code here. Read input from STDIN. Print output to STDOUT
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan(){
fmt.Println("Hello, World.\n"+scanner.Text())
}
}
/********************************************************************************/
Day 1: Data Types
package main
import (
"fmt"
"os"
"bufio"
"strconv"
)
func main() {
var _ = strconv.Itoa // Ignore this comment. You can still use the package "strconv".
var i uint64 = 4
var d float64 = 4.0
var s string = "HackerRank "
scanner := bufio.NewScanner(os.Stdin)
// Declare second integer, double, and String variables.
var (
secondInt uint32
secondDub float32
secondStr string
)
// Read and save an integer, double, and String to your variables.
fmt.Scan(&secondInt)
fmt.Scan(&secondDub)
if scanner.Scan() {
secondStr = scanner.Text()
}
// Print the sum of both integer variables on a new line.
sum := secondInt + i
fmt.Println(sum)
// Print the sum of the double variables on a new line.
sumDub := secondDub + d
fmt.Println(fmt.Sprintf("%.1f",sumDub))
// Concatenate and print the String variables on a new line
// The 's' variable above should be printed first.
fmt.Println(s+secondStr)
}
/*****************************************************************************/
Day 2: Operators
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
/*
* Complete the 'solve' function below.
*
* The function accepts following parameters:
* 1. DOUBLE meal_cost
* 2. INTEGER tip_percent
* 3. INTEGER tax_percent
*/
func solve(meal_cost float64, tip_percent int32, tax_percent int32) {
// Write your code here
perct := tip_percent+tax_percent
val := 1+float64(perct)*1e-2 //保留小数点后2位
res := int32((meal_cost*val)+0.5) //对结果四舍五入
fmt.Println(res)
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
meal_cost, err := strconv.ParseFloat(strings.TrimSpace(readLine(reader)), 64)
checkError(err)
tip_percentTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
tip_percent := int32(tip_percentTemp)
tax_percentTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
tax_percent := int32(tax_percentTemp)
solve(meal_cost, tip_percent, tax_percent)
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
/***********************************************************************************/
Day 3: Intro to Conditional Statements
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
NTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
N := int32(NTemp)
switch {
case N%2!=0:
fmt.Println("Weird")
case N%2==0:
switch{
case N >2 && N <= 5:
fmt.Println("Not Weird")
case N >= 6 && N <= 20:
fmt.Println("Weird")
case N > 20:
fmt.Println("Not Weird")
}
}
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
/**********************************************************************************/
Day 4: Class vs. Instance
package main
import "fmt"
type person struct {
age int
}
func (p person) NewPerson(initialAge int) person {
//Add some more code to run some checks on initialAge
if initialAge < 0{
p.age = 0
fmt.Println("Age is not valid, setting age to 0.")
}else{
p.age = initialAge
}
return p
}
func (p person) amIOld() {
//Do some computation in here and print out the correct statement to the console
if p.age < 13{
fmt.Println("You are young.")
}else if p.age < 18 && p.age >= 13{
fmt.Println("You are a teenager.")
}else if p.age >= 18{
fmt.Println("You are old.")
}
}
func (p person) yearPasses() person {
//Increment the age of the person in here
p.age += 1
return p
}
func main() {
var T,age int
fmt.Scan(&T)
for i := 0; i < T; i++ {
fmt.Scan(&age)
p := person{age: age}
p = p.NewPerson(age)
p.amIOld()
for j := 0; j < 3; j++ {
p = p.yearPasses()
}
p.amIOld()
fmt.Println()
}
}
/****************************************************************************************/
Day 5: Loops
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
nTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
n := int32(nTemp)
for i:=1;i<=10;i++{
res := n*int32(i)
fmt.Printf("%v x %v = %v\n",n,i,res)
}
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
/***********************************************************************/
Day 6: Let's Review
package main
import "fmt"
func main() {
//Enter your code here. Read input from STDIN. Print output to STDOUT
var (T int
s string
res string
s1 string
s2 string)
fmt.Scan(&T)
for i := 0;i < T;i++{
fmt.Scan(&s)
for k,v := range s{
if k % 2 == 0{
s1 = s1 + string(v)
}else{
s2 = s2 + string(v)
}
}
res = s1 +" "+ s2
fmt.Println(res)
s1 = ""
s2 = ""
}
}
/*******************************************************************************/
Day 7: Arrays
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
nTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
n := int32(nTemp)
arrTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
var arr []int32
var res string
for i := 0; i < int(n); i++ {
arrItemTemp, err := strconv.ParseInt(arrTemp[i], 10, 64)
checkError(err)
arrItem := int32(arrItemTemp)
arr = append(arr, arrItem)
}
for i:=len(arr)-1;i>=0;i-- {
res = res + strconv.Itoa(int(arr[i]))+" "
}
fmt.Println(res)
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
/**********************************************************************************************/
Day 8: Dictionaries and Maps
package main
import ("fmt"
"bufio"
"io"
"os"
"strings"
"strconv")
func main() {
//Enter your code here. Read input from STDIN. Print output to STDOUT
reader := bufio.NewReaderSize(os.Stdin,1024*1024)
nTemp , err := strconv.ParseInt(readLine(reader), 10, 64)
n := int(nTemp)
checkError(err)
res := make(map[string]string)
for i := 0; i < n; i++{
arr := strings.Split(readLine(reader)," ")
res[arr[0]] = arr[1]
}
for{
name := readLine(reader)
if name == ""{
break
}
if val,ok := res[name];ok{
fmt.Printf("%s=%s\n",name,val)
}else{
fmt.Println("Not found")
}
}
}
func readLine(reader *bufio.Reader) string{
str, _, err := reader.ReadLine()
if err == io.EOF{
return ""
}
return strings.TrimRight(string(str),"\r\n")
}
func checkError(err error){
if err != nil{
panic(err)
}
}
/****************************************************************************************/
Day 9: Recursion 3
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
/*
* Complete the 'factorial' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER n as parameter.
*/
func factorial(n int32) int32 {
// Write your code here
if n <= 1{
return 1
}
return factorial(n-1)*n
}
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
stdout, err := os.Create(os.Getenv("OUTPUT_PATH"))
checkError(err)
defer stdout.Close()
writer := bufio.NewWriterSize(stdout, 16 * 1024 * 1024)
nTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
n := int32(nTemp)
result := factorial(n)
fmt.Fprintf(writer, "%d\n", result)
writer.Flush()
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
/******************************************************************************************/
Day 10: Binary Numbers
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
nTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
n := int64(nTemp)
baseTwo := string(strconv.FormatInt(n,2))
var local, max int
for i := range baseTwo{
if baseTwo[i] == '1'{
local++
if local > max{
max = local
}
}else{
local = 0
}
}
fmt.Println(max)
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
/*****************************************************************************/
Day 11: 2D Arrays
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
var arr [][]int32
for i := 0; i < 6; i++ {
arrRowTemp := strings.Split(strings.TrimRight(readLine(reader)," \t\r\n"), " ")
var arrRow []int32
for _, arrRowItem := range arrRowTemp {
arrItemTemp, err := strconv.ParseInt(arrRowItem, 10, 64)
checkError(err)
arrItem := int32(arrItemTemp)
arrRow = append(arrRow, arrItem)
}
if len(arrRow) != 6 {
panic("Bad input")
}
arr = append(arr, arrRow)
}
fmt.Print(findMaxHourGlasses(arr))
}
func findMaxHourGlasses(arr [][]int32) int32{
var max int32
max = -2147483648 //zero value will be the max of negetives
for x := 1; x < len(arr)-1; x++{ //outer loop denotes column num, due to hourglass structure, the column only can move len(arr)-2 loops
for y := 1; y < len(arr[x])-1; y++{ //inner loop denotes row num
var total int32 //tmp value used for sum must in the inner loop to compare with max
//up
up := arr[y-1][x-1:x+2]
for _,element := range up{
total += element
}
//self
center := arr[y][x]
total += center
//bottom
bottom := arr[y+1][x-1:x+2]
for _,element := range bottom{
total += element
}
if total > max{
max = total
}
}
}
return max
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
/*********************************************************************************/
Day 12: Inheritance (Golang does not have the property, here is python3 solution)
class Person:
def __init__(self, firstName, lastName, idNumber):
self.firstName = firstName
self.lastName = lastName
self.idNumber = idNumber
def printPerson(self):
print("Name:", self.lastName + ",", self.firstName)
print("ID:", self.idNumber)
class Student(Person):
# Class Constructor
#
# Parameters:
# firstName - A string denoting the Person's first name.
# lastName - A string denoting the Person's last name.
# id - An integer denoting the Person's ID number.
# scores - An array of integers denoting the Person's test scores.
#
# Write your constructor here
def __init__(self,firstName,lastName,idNumber,testScore):
Person.__init__(Person,firstName,lastName,idNumber)
self.testScore=testScore
# Function Name: calculate
# Return: A character denoting the grade.
#
# Write your function here
def calculate(self):
average=sum(self.testScore)/len(self.testScore)
if average>=90 and average<=100:
return "O"
elif average>=80 and average<90:
return "E"
elif average>=70 and average<80:
return "A"
elif average>=55 and average<70:
return "P"
elif average>=40 and average<50:
return "D"
elif average<40:
return "T"
line = input().split()
firstName = line[0]
lastName = line[1]
idNum = line[2]
numScores = int(input()) # not needed for Python
scores = list( map(int, input().split()) )
s = Student(firstName, lastName, idNum, scores)
s.printPerson()
print("Grade:", s.calculate())
/***********************************************************/
Day 13: Abstract Classes
from abc import ABCMeta, abstractmethod
class Book(object, metaclass=ABCMeta):
def __init__(self,title,author):
self.title=title
self.author=author
@abstractmethod
def display(): pass
#Write MyBook class
class MyBook(Book):
def __init__(self, title,author,price):
self.price = price
super(MyBook,self).__init__(title,author)
def display(self):
print ("Title: %s" %(self.title))
print ("Author: %s" %(self.author))
print ("Price: %s" %(self.price))
title=input()
author=input()
price=int(input())
new_novel=MyBook(title,author,price)
new_novel.display()
/*******************************************************************************/
Day 14: Scope
class Difference:
def __init__(self, a):
self.__elements = a
self.maximumDifference = None
def computeDifference(self):
self.maximumDifference = max(self.__elements) - min(self.__elements)
# Add your code here
# End of Difference class
_ = input()
a = [int(e) for e in input().split(' ')]
d = Difference(a)
d.computeDifference()
print(d.maximumDifference)
/***********************************************************************************/
Day 15: Linked List
class Node:
def __init__(self,data):
self.data = data
self.next = None
class Solution:
def display(self,head):
current = head
while current:
print(current.data,end=' ')
current = current.next
def insert(self,head,data):
#Complete this method
if head is None:
return Node(data)
head.next = self.insert(head.next,data)
return head
mylist= Solution()
T=int(input())
head=None
for i in range(T):
data=int(input())
head=mylist.insert(head,data)
mylist.display(head);
/*****************************************************************/
Day 16: Exceptions - String to Integer
#!/bin/python3
import math
import os
import random
import re
import sys
S = input().strip()
try:
print (int(S))
except ValueError:
print ("Bad String")
/********************************************************************/
Day 17: More Exceptions
#Write your code here
class Calculator():
def power(self, n, p):
if (n < 0) or (p < 0):
raise Exception('n and p should be non-negative')
return n**p
myCalculator=Calculator()
T=int(input())
for i in range(T):
n,p = map(int, input().split())
try:
ans=myCalculator.power(n,p)
print(ans)
except Exception as e:
print(e)
/***********************************************************************/
Day 18: Queues and Stacks
import sys
class Solution:
# Write your code here
# Declare two instance variables
def __init__(self):
self.stack = []
self.queue = []
# Adds an item to the end of the list
def pushCharacter(self, item):
self.stack.append(item)
# Removes an item from the end of the list
def popCharacter(self):
return self.stack.pop()
# Adds an item to the end of the list
def enqueueCharacter(self, item):
self.queue.append(item)
# Removes an item from the front of the list
def dequeueCharacter(self):
return self.queue.pop(0)
# read the string s
s=input()
#Create the Solution class object
obj=Solution()
l=len(s)
# push/enqueue all the characters of string s to stack
for i in range(l):
obj.pushCharacter(s[i])
obj.enqueueCharacter(s[i])
isPalindrome=True
'''
pop the top character from stack
dequeue the first character from queue
compare both the characters
'''
for i in range(l // 2):
if obj.popCharacter()!=obj.dequeueCharacter():
isPalindrome=False
break
#finally print whether string s is palindrome or not.
if isPalindrome:
print("The word, "+s+", is a palindrome.")
else:
print("The word, "+s+", is not a palindrome.")
/***************************************************************************************************/
Day 19: Interfaces
class AdvancedArithmetic(object):
def divisorSum(n):
raise NotImplementedError
class Calculator(AdvancedArithmetic):
def divisorSum(self, n):
factorslist=[]
for i in range(1,n+1):
if n%i==0:
factorslist.append(i)
return sum(factorslist)
n = int(input())
my_calculator = Calculator()
s = my_calculator.divisorSum(n)
print("I implemented: " + type(my_calculator).__bases__[0].__name__)
print(s)
/******************************************************************************/
Day 20: Sorting Golang Solution
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReaderSize(os.Stdin, 16 * 1024 * 1024)
nTemp, err := strconv.ParseInt(strings.TrimSpace(readLine(reader)), 10, 64)
checkError(err)
n := int32(nTemp)
aTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
var a []int32
for i := 0; i < int(n); i++ {
aItemTemp, err := strconv.ParseInt(aTemp[i], 10, 64)
checkError(err)
aItem := int32(aItemTemp)
a = append(a, aItem)
}
// Write your code here
bubbleSort(a)
}
func bubbleSort(arr []int32) []int32{
n := len(arr)
swapCount := 0
for i := 0; i < n; i++ {
// Track number of elements swapped during a single array traversal
numberOfSwaps := 0
for j := 0; j < n - 1; j++ {
// Swap adjacent elements if they are in decreasing order
if arr[j] > arr[j + 1] {
arr[j], arr[j + 1] = arr[j + 1], arr[j]
numberOfSwaps++
swapCount++
}
}
// If no elements were swapped during a traversal, array is sorted
if (numberOfSwaps == 0) {
break;
}
}
fmt.Printf("Array is sorted in %d swaps. \n",swapCount)
fmt.Printf("First Element: %d \n",arr[0])
fmt.Printf("Last Element: %d \n",arr[n-1])
return arr
}
func readLine(reader *bufio.Reader) string {
str, _, err := reader.ReadLine()
if err == io.EOF {
return ""
}
return strings.TrimRight(string(str), "\r\n")
}
func checkError(err error) {
if err != nil {
panic(err)
}
}
/******************************************************************/
Day 21: Generics (C++ solution, Golang 1.8 version begin to support generics)
#include <iostream>
#include <vector>
#include <string>
using namespace std;
/**
* Name: printArray
* Print each element of the generic vector on a new line. Do not return anything.
* @param A generic vector
**/
// Write your code here
template<class T>
void printArray(vector<T>arr){
for (int i = 0; i < arr.size(); i++) {
cout<<arr[i]<<endl;
}
}
int main() {
int n;
cin >> n;
vector<int> int_vector(n);
for (int i = 0; i < n; i++) {
int value;
cin >> value;
int_vector[i] = value;
}
cin >> n;
vector<string> string_vector(n);
for (int i = 0; i < n; i++) {
string value;
cin >> value;
string_vector[i] = value;
}
printArray<int>(int_vector);
printArray<string>(string_vector);
return 0;
}
/********************************************************************************************/
Day 22: Binary Search Trees
class Node:
def __init__(self,data):
self.right=self.left=None
self.data = data
class Solution:
def insert(self,root,data):
if root==None:
return Node(data)
else:
if data<=root.data:
cur=self.insert(root.left,data)
root.left=cur
else:
cur=self.insert(root.right,data)
root.right=cur
return root
def getHeight(self,root):
#Write your code here
if root == None:
return -1
else:
ldepth = self.getHeight(root.left)
rdepth = self.getHeight(root.right)
if ldepth>rdepth:
return ldepth+1