0% found this document useful (0 votes)
39 views12 pages

Binary Search Trees: Operations & Problems

Uploaded by

srk712906
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views12 pages

Binary Search Trees: Operations & Problems

Uploaded by

srk712906
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lecture 57

Binary Search
Trees - 3
Recap
● Searching, Insertion, Deletion
● Interview Problems on BST
Ques: Trim a Binary Search Tree [LeetCode 669]

if (relebt eval <lo)[


⑳ weri
releft= ve e
left ight

rig ↓
3
⑰ hi)[


if (resightsval>
↓ ve right-root right eleft
3

10
in i
1
=

=3
-s
Ques: Trim a Binary Search Tree [LeetCode 669]

wie mdumn
d
t if (root-left eval

roots left-roots
<lo)

left enight

-
10 2,
=
hi 4
=
Ques: Trim a Binary Search Tree [LeetCode 669]

5
·O
I
8 4

lo 1
=

10 1
=

ni 2 =

hi =4

if(relefteval> his

~eleft-
we
left left
Morris Traversal = Inorder Traversal

Tre,sn,t-
!
Recursive/sterative
-

P.C. 0(n)
=
S.c. 0(h) /0(m)
=

terative inorder traversal e S.C. 0(1)


=

start can root


with

41) exists
curve let
-
pred
link
unlink & visit
NULL - visit (surv)
2) care left ==

cut=curve
right
Morris Traversal e Predecessor (Inorder)
we ne

while (Curr!=NULL)[
2

if(car-left!=NULL//find pred

⑩ cawsleft
pred=

-
while Iprederigt!- NULL &&peright?=()
S pred=prederight

d
N
if (prederight ==NULL) //link
③ I pred right c as
=

" 3
cur= wwe

==c)
left

[X/unlink
it (prode right
10 15 1828
prode right NULL
=

visit (curr ↓
viscer
cese

ele
car: cr-right
care
cur:
right
3 3
Ques: Flatten Binary Tree to Linked List [LeetCode 114]

O preorder vector
M- I
-
: Make a
of

② -
⑤ treenodes.

③ ⑭ wo
-

Vector, TreeNodet> ans [


=
1, 2, 3, 4, 5, 63

) s.C 0(n)
=
Ques: Flatten Binary Tree to Linked List [LeetCode 114]

M2: sion: Preorders Root


Right
Left
root
root
root
①* e

Os
2
l

O
②zug
*
S
B
at
'' I


I

2
d
⑤ S
vos ⑧ 38
l =

left
root right

T.C 0(n)
=

r
=

S.2 0(n)
=
Ques: Flatten Binary Tree to Linked List [LeetCode 114]
M:Morris Traversal (1) curr,
pred, right

NVLL)[I find pred &mark


root if (celeft!=
OI r cur
=

right

⑳ ⑤ cure right cure left


=

- d finding pred
P
Bu ⑥ -
Linke cred right 2
=

C curr-care left
3
else cuire
cur= right
Next Lecture
v
● Sets, Maps Heaps
·
THANK YOU

You might also like