TRANSCRIPTEnglish

Convert Binary Tree to Doubly Linked List - FAANG Interview Question

10m 32s1,339 words214 segmentsEnglish

FULL TRANSCRIPT

0:03

hey everyone welcome back to my channel

0:05

in the new video in this video we're

0:06

continuing the data algorithms boot camp

0:09

and we're doing another nice uh question

0:10

of binary trees in this uh the question

0:13

says convert a binary tree into a dou

0:18

link list so if you don't know what a

0:19

dou link list is I would encourage watch

0:22

my link list introduction playlist it's

0:24

very simple we will have a head and a

0:26

tail and you have uh you know we have

0:30

have link list like

0:39

this

0:42

head tail and it will also be pointing

0:46

to the previous node so every node is

0:48

pointing to the node in front of it and

0:52

also behind it okay so that's what a w

0:56

link list is and we are given head and

0:59

tail

1:01

and uh well we are not given head and

1:03

tail we have to create it for example

1:05

but we are given a binary tree so we're

1:07

going to tree like

1:16

this

1:20

okay this

1:22

is five

1:26

six this is let's

1:28

say three

1:30

this is let's say one this is let's say

1:36

two no two

1:38

four this is let's say

1:41

two okay this is

1:44

one so you are to convert this tree into

1:48

a w link list how are we going to do

1:50

this let's say it is a binary search

1:52

tree so you have to convert it into a w

1:54

link list that is sorted so obviously we

1:57

are going to use traversals but the

1:58

question is which kind of Tri Ral will

2:00

you be using what is the traversal that

2:02

gives us the sorted order in a binary

2:04

search tree in order traversal so you

2:07

start from here will I take this no I

2:09

will go in the left I will go in the

2:11

left I will go in the left null so I

2:13

will then print the current

2:15

node which is

2:17

one then I will go write nothing then I

2:20

will go here bring the current node then

2:24

um right then I will go back left is

2:27

printed now I'll print the current node

2:29

three

2:31

then uh

2:32

right okay then the left is printed I

2:36

will print the current node which is

2:39

five and then

2:42

write in ordered

2:45

reversal we all know what inod reversal

2:47

is just watch the link list tutorial

2:49

video if you haven't already and if

2:51

you're having doubts in this but uh this

2:53

is basically how it would work now the

2:56

question is very simple we will do in

2:58

order reversal but while we are printing

3:00

it we will build our dou link list so

3:04

when I printed this one I will check is

3:07

head equal to null yes currently head is

3:09

equal to null because the list is empty

3:12

so I will assign this as

3:14

head then I will Traverse again now I

3:17

have

3:18

two so I will be like is head equal to

3:20

null no so I will be like the current

3:23

pointer that I have let's say tail is

3:25

also equal to this I will say tail. next

3:28

is equal to this and this currently

3:29

previous is equal to the original tail

3:31

that I had and then tail will be updated

3:34

to the new one then I will print three

3:37

we like okay tail do next is equal to

3:39

the current node this current node.

3:41

previous is equal to the previous

3:44

tail okay very easy stuff and until then

3:49

we will move forward pause this video

3:51

Try it yourself and then uh look at the

3:54

code solution okay so I'm going to go to

3:57

my folder by the way you can check out

3:59

the link link to the code in the

4:00

description below going to create a

4:04

new Java file or I'm just going to go to

4:07

the advanced three questions

4:10

one and create a new

4:13

file

4:14

double link list.

4:19

Java zoom in a little

4:28

bit

4:31

double linked list

4:35

cool so that's what we have here what is

4:37

the double link list going to have uh it

4:40

is going to have as you can see over

4:42

here head uh the each node if we talk

4:46

about the

4:47

node uh what is the node going to have

4:51

so let's say I create a new node over

4:55

here let's say we

4:58

have a new

5:00

node uh I'm going to call

5:02

it just class node this is going to have

5:07

in a value node previous node next

5:16

okay public

5:20

node Constructor just going to pass

5:24

value this. value is equal to Value okay

5:29

this is my node uh I'm going to call

5:33

this one actually link list

5:37

node and I'm going to

5:39

create let's say we create a tree node

5:43

also trees have what left and right in

5:51

val always make sure when you copy paste

5:57

tree node left tree node

6:01

right public tree

6:04

node in

6:07

well

6:10

Constructor

6:13

done

6:15

cool sounds good now the W link list

6:18

that we have over here I'm going to

6:21

create what head and tail so I'm going

6:26

to say two

6:28

types tree

6:30

node head and tree

6:33

node

6:35

tail sounds

6:38

good after this I'm going to

6:41

have what am I going to

6:44

have

6:47

converting how do I create it I have to

6:49

convert it from a binary tree it says

6:51

binary search tree is given convert into

6:53

a w link okay

6:56

public it will return three

7:00

node

7:02

convert

7:07

what tree node root by the way this will

7:11

not be tree node this will be linkless

7:14

node not a tree head and tail tree is

7:17

left and right link list head L link

7:20

list tail here on the screen you can

7:23

see it is saying uh convert tree node

7:28

root

7:30

sounds good now some basic checks if

7:33

root is equal to equal to

7:36

null return

7:40

null otherwise I'm going to call my

7:42

helper function pass my root node in

7:47

it okay then I'm going to say return

7:53

head sounds

7:57

good don't have to make it public I can

8:00

say private void

8:03

helper helper takes in tree node

8:07

node not a

8:10

problem okay I can say if node is equal

8:13

to equal

8:15

to

8:17

null return null otherwise in ordered

8:19

reversal so what I'm going to do is I'm

8:23

going to call the left

8:25

first node. left I will call first then

8:29

I will

8:30

call the current and then in the end

8:33

node.

8:34

write okay so when I talk about the

8:38

current let's say you have found the

8:39

answer let's say this is the one so what

8:41

do what do we do we create the new node

8:44

so link list

8:45

node um new node is equal to New Link

8:50

list node and the value will be what

8:53

node

8:54

dot value now if head is equal to to

8:59

equal to

9:02

null head is equal to new node tail is

9:07

also equal to new node only one

9:09

element

9:11

okay otherwise tail. next is equal to

9:15

new

9:16

node and then tail itself is equal to

9:19

that new node what we just

9:25

saw but before that so take next is

9:29

equal to new node new node. previous is

9:32

equal to tail also is

9:34

left new node do previous is equal to

9:38

tail is also left and tail is equal to

9:40

new

9:41

node and then helper node.

9:45

right uh that's it

9:49

done pretty simple

9:52

stuff cool cool this uh code that you

9:55

can see I will leave the link in the

9:57

description as well you can check it out

9:59

all right so that was it pretty short

10:00

question but very simple may have looked

10:03

intimidating but it was not uh the key

10:06

how did I know what to do is uh the hint

10:08

I was like you have to do a sending

10:09

order that's why we did in ordered

10:11

reversal and we did exactly what in Ed

10:12

reversal says do the left

10:15

first then do the current node whichever

10:18

one you're at then do the right simple

10:21

stuff if this was unclear you can check

10:23

out the previous videos and I'll leave

10:25

the links in the description below

10:26

thanks a lot for

10:28

watching

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.