• 2 days ago
Transcript
00:00Next, we will talk about the next question. It's called Root to Leaf Paths.
00:04It means, based on a tree, based on the root of BST,
00:07we have to print all the paths.
00:10First, let's analyze what paths are.
00:13The root is 8, so we will start with 8.
00:15The first path goes from 5 to 3.
00:19So, 8, 5 and 3.
00:22The second path goes from 8, 5 and 6.
00:25So, 8, 5 and 6.
00:27The third path goes from 8 to 10, 10 to 11, 11 to 14.
00:32So, 8, 10, 11 and 14.
00:34Let's take these three paths to root.
00:38Now, what will be our approach?
00:40The approach will be that we will travel to root and leave.
00:44We will travel to the nodes.
00:46We will add to our paths.
00:48First, we have to decide a data structure.
00:51We will use it to store our paths.
00:53So, what will we use in the data structure?
00:57We will use an array list.
01:00We will take an array list.
01:01We will put the name of the array list.
01:05We will draw it.
01:08We will store our paths.
01:15Why did we choose the array list?
01:16Why did we choose here?
01:18Because we need a data structure that is flexible.
01:20We can delete elements and add elements.
01:24Because our paths will change as we reach a new leaf.
01:28So, what do we need to do?
01:30In the beginning, we will go to the node.
01:32The first step is to add that node.
01:37Add node to path.
01:41So, we will start with root.
01:45So, we will add 8 to our paths.
01:48After that, we will go to the left subtree.
01:50So, the second step is to travel the left subtree.
01:56We will go to the left subtree.
01:58We will add 5 to 5.
02:00So, we will add 5 to the path.
02:02After that, we will add 5 to the same thing.
02:05We will go to the left subtree.
02:06We will reach 3 to the left subtree.
02:07So, we will add 3 to the path.
02:10We will also add 3 to the path.
02:10As we know that we have reached leaf.
02:13That case came from leaf.
02:16So, what do we do?
02:17Print path.
02:21We will print the path.
02:23we will print take much further and then we will kannst it off.
02:25We will print first 8.
02:278 came from 8.
02:28Then we will print 5.
02:29Then we will print 3 to 5.
02:32So, that� tomorrow we will print 3.
02:33So, the path 1 will print.
02:34So, how did the path?
02:36With our Path Array List, we will traverse the path within the list.
02:39Then we will print the path.
02:41After that, we will backtrack.
02:43We will turn off to the parents.
02:46We will clear the leaves back.
02:47We will go back to 5. This is our backtracking step.
02:53We will remove the Array list.
02:55Because we have traveled the 3 path and 3 nodes.
02:59We will delete the Array list.
03:01We will remove the Array list from here.
03:05We will go back to 5. We will go back to the right subtree.
03:10The next step is to go back to the right subtree.
03:17We will create a left loop.
03:17We will add 6 to the right subtree.
03:21Now we discovered that 6 is a leaf node.
03:25We will print our path as a leaf node.
03:27Then we will print which way to our path?
03:29We will print 8.
03:29Then we will print 5.
03:31Then 5 to our left.
03:34Then we will go back to the next loop.
03:38We will remove the 5th to our path.
03:41We will return the 5th.
03:41We will also remove the 6th.
03:43So we can also remove the 5th.
03:45We are coming back to 8
03:48What do we want to do to do to 8?
03:50We have been traveling to 8 left subtree
03:52Now we are coming to 8 right subtree
03:54Step 3
03:55We will add 10 to the right subtree
03:59We will add 10 to the path
04:00We will add 10 to the right subtree
04:02We will add 11 to the path
04:04We will add 14 to the path
04:08As we have 14
04:10We will print the path
04:12How do we print the path?
04:13First we will print 8
04:15Then 10
04:16Then 11
04:17Then 14
04:19We will print the path 3
04:21Now we will backtrack
04:22As we will backtrack
04:24We will delete our notes
04:28Finally
04:29As we reach 8
04:31We will backtrack to 8
04:34So 8 will also delete
04:35And last we will empty
04:37So what steps we have followed?
04:40They were very easy
04:40You will see the steps
04:42Pre-order traversal
04:44First we have to add your node
04:47Then we have to travel to the left subtree
04:49Then we have to travel to the right subtree
04:50And then we have to remove the node
04:52As we will backtrack
04:54As we have reached the leaf
04:55As we have reached the base case
04:56As we have reached the leaf
04:57We will print the path
04:59So this whole pre-order traversal
05:02We have to write the code
05:04We have to create a function
05:05Public static
05:08Void
05:09Print
05:11Root
05:12To leaf
05:13Here we have our parameters
05:15We have our root
05:16And above that
05:17We have an array list
05:18Array list of integers
05:20Which we will give the path
05:23This is the red line
05:24That's why we are coming
05:25Because we have not imported
05:26Our array list
05:27Package
05:27Utility package
05:28So we will import
05:30Import
05:31Java
05:33.util
05:33All
05:41All
05:42What will be the first step?
05:43The first step is
05:45That our current node
05:47Add the path
05:48So path.add
05:50Root.data
05:53The second step is
05:56The second step is
05:58The left subtree
05:59To reach the left subtree
06:01We will call
06:01Print root
06:03To leaf
06:04For left subtree
06:07Then we will call
06:08Print root
06:09To leaf
06:09For right subtree
06:12Now
06:13Add
06:14To the left subtree
06:15Printed
06:15To the right subtree
06:16To the right subtree
06:17Now we have to remove
06:18To remove
06:20We can pass the path
06:21Or we can pass the object
06:22Or we can pass the index
06:23To which we have to delete
06:25Now we know
06:25In the path
06:26The last element added
06:27It was us
06:28It was our root
06:29To the left subtree
06:31We will zoom in
06:32To the left subtree
06:33So we have to remove
06:33To the right subtree
06:35As we have to remove
06:35As we have to remove
06:36The right subtree
06:36The other nodes added
06:36The other nodes
06:38The other nodes
06:40Is the root node
06:41The data is the last node
06:42That exists in our index
06:44To the last node
06:44So we will pass
06:45Path.size
06:46To the left subtree
06:47Is minus 1
06:50And return
06:51From here
06:51How will the base case
06:52How will the handle
06:53How will the base case
06:54How will the case
06:55How will the case
06:56How will the case
06:59The left root
07:00Is also null
07:01In which
07:02We are on the leaf node
07:02And the root
07:04Is also null
07:05In that case
07:08What will we do
07:08In that case
07:09We will print
07:09Path
07:10So we will create
07:11Utility function
07:13Print Path
07:16The root
07:18The root
07:19In that case
07:19If the root
07:20Is null
07:24We have to call
07:25To the null node
07:26So we will simply
07:27Return
07:29This print path
07:30Function
07:30Public
07:32Static
07:32Void
07:33Print Path
07:35We will have
07:36Our
07:39ArrayList
07:42What will we do
07:44In that case
07:44We will do
07:44A loop
07:54Put
07:55Path
07:56Get
07:57I
07:57Path
08:00I
08:02Path
08:03I
08:04Get
08:04We have
08:04Printed
08:05In fact
08:05Here
08:06A arrow
08:13To
08:14I haveふふN
08:15monkeyvel
08:16N件
08:16point
08:16Heart
08:18Remove
08:19Head
08:19We will add all the elements to the path.
08:22As we press the leaf, we will print the path.
08:25Then we will add calls to the left and right sub-tree.
08:28Then we will finally remove the path from the current route.
08:32We will add call to the main function.
08:34We will print root to the leaf.
08:38And we will pass a new array list.
08:49So 4 paths are printed for our tree.
08:52This tree is our old tree.
08:55If we have taken the example of the new tree.
09:07If we print paths for this,
09:10we will have 3 paths.
09:12853, 856 and 810, 11 and 14.