TRANSCRIPTEnglish

Correct Binary Tree That Has Two Nodes Swapped - FAANG Interview Question

10m 32s1,373 words216 segmentsEnglish

FULL TRANSCRIPT

0:04

hey everyone welcome back to my channel

0:05

the new video in this video we're

0:07

covering another nice uh question of a

0:09

binary search tree which is uh you're

0:12

given a binary search tree and uh let me

0:16

just bring up my iPad here

0:19

okay so you are given a binary search

0:28

tree let's say you have

0:32

five

0:34

7

0:37

6 this would be let's say

0:42

8

0:43

3 4 2 1 let's say this is the binary

0:49

search tree that is given to you what

0:52

the problem is that

0:54

uh two nodes in the binary search tree

0:57

are swapped it can be any two

1:00

nodes it can be any two nod so let's say

1:05

this one this one are

1:06

swapped

1:08

7 and two okay so these two nodes are

1:11

swapped now it can be any two nodes we

1:14

know that uh how are we going to solve

1:17

this one of the ways you can solve this

1:18

is you can imagine that let's say if

1:21

this is a binary search tree and we do

1:23

in ordered reversal then we know that it

1:25

will be a sorted array

1:28

so if it is a sorted array so in order

1:30

reversal before the swapping the sorted

1:34

array would look something like this 1 2

1:38

3 4

1:41

5 6 7 8 this is what it would look like

1:45

but what has now happened is 2 and seven

1:48

have been

1:50

swapped 2 and seven have been swapped so

1:53

this question now becomes that okay in a

1:55

sorted array Which two elements are

1:57

swapped this can be very simple you can

2:00

just check the previous one so there

2:01

will be two violations one violation is

2:05

over here one violation is over

2:07

here s is greater than its previous

2:10

element two is less than its previous

2:15

element okay two

2:17

violations so that is what we have to

2:20

find these two violations we have to

2:24

find in our binary search

2:27

tree so what we're do is we're going to

2:30

take the first violation is equal to

2:32

initially null second violation in equal

2:35

equal to null whenever we find the first

2:37

violation so the first violation will

2:40

arrive when we say that

2:43

the current node that you are at so it's

2:47

in order reversal is what left current

2:49

then right so if I am talking about the

2:51

current node it means left is already

2:53

been visited so I'm going to be like

2:55

let's say I go here then I come here

2:57

then I come here here I say if left

3:01

node is less than the previous

3:06

node it means that first has been found

3:10

which is equal to previous node because

3:12

this is the previous

3:15

one previous

3:17

node and then we know that we will find

3:20

the first one first because it comes

3:21

first so we'll say if first is not equal

3:24

to null means first has been

3:26

found if first not equal to null first

3:32

is equal to previous

3:33

node then we will keep on uh going we

3:36

will now go over here this looks fine we

3:40

will come over

3:42

here then we will come over here this

3:45

looks fine we'll come over here we'll do

3:46

the same condition if this not left it

3:51

is the current node

3:54

current current node that you are at now

3:56

we are at current node if current node

3:58

is less than the previous node

4:00

yes it is in this

4:02

case we will say that the

4:06

second node that is wrong is equal to

4:10

what previous no previous is six the

4:13

current which is the current node

4:18

current and that's it after that we will

4:21

just swap these two because these are

4:23

just pointers to these nodes let

4:25

references to these nodes that's it

4:28

pretty simple I told you when I was

4:30

teaching uh like link list when such

4:33

questions are given you don't have to

4:34

think you just have to do what the

4:35

question is saying and this is what the

4:37

question is

4:42

saying okay you do understand how I'm

4:44

making these previous right so first of

4:45

all I will have whenever I choose this

4:48

this will be previous then I evaluate

4:50

this this will be previous then I

4:53

evaluate this I will do this then this

4:54

will become previous whenever you keep

4:56

evaluating make it previous okay okay if

4:59

a current node is evaluated that is

5:01

previous then I will go here this will

5:04

remain

5:05

previous and this will become previous

5:08

then I will go here so on and so forth

5:11

okay very simple pause this video think

5:13

about it and uh by the what is the time

5:16

complexity going to be for this solution

5:17

we are visiting every node only

5:20

once and uh there's the recursive space

5:24

also so we are taking o of n

5:28

time

5:31

right that's what we're

5:33

doing and uh for the tree we are only

5:37

doing in AIT reversal so it's the height

5:40

of the height of the tree okay so here

5:42

you can see on the screen uh can by the

5:45

way find the link in the description uh

5:47

below as well for this code so the

5:49

question

5:52

was two node swap so we're going to

5:56

create two node swap. Java

6:00

first of all I'm going to say

6:03

class class

6:05

node int

6:08

value node

6:10

left node

6:16

right public node in val this dot val is

6:22

equal to Val The Constructor is done all

6:26

good looking nice

6:30

now public Class 2 node

6:35

swap here I'm going to create my node

6:42

first okay initially that is null node

6:47

second and I'm going to create a node

6:49

previous that is also initially equal to

6:53

null I'm going to say

6:55

public

6:57

void I'm going to call this one

7:00

um

7:02

helper okay and I'm going to pass

7:05

node let's say for the naming convention

7:09

root over here I'm going to call in

7:13

order

7:14

traversal on the root node while when I

7:18

have done in order reversal these two

7:21

will be

7:22

populated so I will just swap so I will

7:27

say

7:28

first or I will just

7:31

say if I want to swap

7:33

it

7:37

um yeah let's say ideally I just swap

7:40

the let's say just the values are

7:43

interchanged okay to make things simpler

7:45

but you can swap if the entire nodes are

7:47

interchanged as well so I can say first

7:51

dot

7:55

val first. Val is equal

7:58

to

8:01

second do

8:04

Val second do Val is equal

8:11

to Temp so okay initially now we we

8:15

assumed that only the values are swapped

8:18

so you can

8:20

uh you know swap the entire nodes as

8:23

well but you are pro experts in that so

8:25

you can do it yourself now private

8:30

void in ordered reversal node node now I

8:34

will just do in ordered

8:35

reversal if node is equal to equal to

8:40

null

8:42

return that's it just

8:44

return in order reversal node do

8:50

left node do right and in the middle we

8:54

will do the work that is what inord

8:55

reversal

8:56

is so what is the work that we're doing

9:00

check if the previous node is violating

9:04

the binary search Tre property so

9:07

if the previous node do value is

9:11

actually greater than the current node

9:13

do value also so that we don't get null

9:16

pointer

9:17

exception if previous node is not equal

9:20

to null

9:22

and then I will check if first node is

9:26

equal to equal to null

9:29

in that case I will say that I have

9:31

found the first violation this thing

9:33

first violation so I will say first is

9:36

equal to previous otherwise I will say

9:39

second is equal to current which is the

9:43

node okay then I will say previous is

9:46

equal to current

9:49

node that's

9:52

it very simple stuff problem

9:55

solved

9:57

okay very cool

10:00

okay that's pretty much about it very

10:01

simple solution and we did a trial run

10:03

as well you can try running it if you

10:04

want uh it's very easy just to create

10:07

two trees and uh if you have any

10:09

questions let me know in the comment

10:10

section below and uh we'll be continuing

10:12

the boot camp and introducing newer

10:14

Concepts um you know in the next new

10:17

year and uh I want to wish you all a

10:19

Happy New Year as well and uh yeah see

10:22

you next year

10:28

bye

UNLOCK MORE

Sign up free to access premium features

INTERACTIVE VIEWER

Watch the video with synced subtitles, adjustable overlay, and full playback control.

SIGN UP FREE TO UNLOCK

AI SUMMARY

Get an instant AI-generated summary of the video content, key points, and takeaways.

SIGN UP FREE TO UNLOCK

TRANSLATE

Translate the transcript to 100+ languages with one click. Download in any format.

SIGN UP FREE TO UNLOCK

MIND MAP

Visualize the transcript as an interactive mind map. Understand structure at a glance.

SIGN UP FREE TO UNLOCK

CHAT WITH TRANSCRIPT

Ask questions about the video content. Get answers powered by AI directly from the transcript.

SIGN UP FREE TO UNLOCK

GET MORE FROM YOUR TRANSCRIPTS

Sign up for free and unlock interactive viewer, AI summaries, translations, mind maps, and more. No credit card required.