- 2 days ago
Transcript
00:00Now, what will I do to remove top view of a binary tree?
00:02I will traverse my tree on my level-wise.
00:05What can I say?
00:07The first node with unique horizontal distance is the top view of the binary tree.
00:16I will show you a little dry run.
00:20I have started to traverse the tree on my level-wise.
00:23How did I traverse my level order traversal?
00:27We have studied the level order traversal in a very beginning
00:30that the tree is a level-wise.
00:33We use the queue data structure.
00:36When I traverse my level-wise, I will go to my one.
00:40I will calculate the horizontal distance to one.
00:44The horizontal distance will come from zero.
00:46I will take a map to track this horizontal distance.
00:51The horizontal distance is unique.
00:53It is unique.
00:53Or the first time is coming.
00:55Or the first time is coming.
00:55Or it is already coming.
00:56And this horizontal distance is already a node that I have printed.
01:01I will create a map.
01:03I will create a structure.
01:06In which one direction will come from my horizontal distance.
01:10The key will become the key.
01:11Because I need a horizontal distance unique.
01:13And in my map, the keys are unique values.
01:17The second time will come, I have my node.
01:20Which will become my value.
01:22That means, for every horizontal distance corresponding,
01:25I will store a node in which the horizontal distance will come from the first time.
01:30In the level order traversal.
01:31In the level order traversal, first of all, I will go to my root node 1.
01:35For 1, I have calculated the horizontal distance as 0.
01:38Now, I will check that the map already exists.
01:41The horizontal distance has never come from 0 horizontal distance.
01:43The root node has never come before.
01:45So, the map is unique.
01:47So, I will add it to my map.
01:50So, if the next time there is 0,
01:52it will not be added to the answer.
01:53And I will store my root node here.
01:56Which is equal to 1.
01:58Besides, I will also take a minimum and maximum value.
02:02With the help of a min-max,
02:03I will track how much my minimum and maximum horizontal distance is coming from.
02:07Now, the minimum is also 0.
02:09The maximum is also 0.
02:11I have to do level order traversal.
02:12For 1, I have already seen that
02:15the horizontal distance of 1 came from 0.
02:17This horizontal distance didn't come before any value.
02:21That means, the map was empty.
02:22It was empty.
02:23So, how do we check?
02:24I can check.
02:25I can check.
02:26Map.get
02:26I will pass it to 0.
02:29If 0 has returned a valid value,
02:32then it means that I have horizontal distance exists.
02:35And if I can't do value return,
02:37then it means that I have horizontal distance exists.
02:39So, in the map of 0,
02:40there will not be valid value return.
02:43That's why I have horizontal distance is unique.
02:47So, I have added 0.
02:48I have added my root node.
02:50Now, I will travel to level-wise,
02:50then I will come to our second level.
02:53Like I have seen 1.
02:54What will I do?
02:551 will process my 2 child.
02:57Then I will process my right child 3.
02:59So, in the process,
03:00the 2 child will process first.
03:02The horizontal distance will be equal to 2.
03:05Then, the 2 will check.
03:06Does the 2 already exist in the map?
03:09It doesn't exist in the map.
03:10What will I do?
03:11I will add 2.
03:12I will add 2 in the map.
03:14Minus 1 will become my horizontal distance.
03:17And the 2 will become my value.
03:20Then, what will I do?
03:21I will process 3.
03:22Because in level-order traversal,
03:24there is a level-wise node process.
03:26After that, we go deeper level.
03:28Because level-order traversal is a BFS technique.
03:31BFS means breadth first technique.
03:33In which we traverse our siblings first.
03:36After that, we go deeper.
03:38So, first of all the nodes will process all this level.
03:41Then, this level.
03:42Then, this level.
03:43Then, this level.
03:43Then, this level.
03:44If we are doing DFS,
03:46then it will be 2 process.
03:47Then, 4.
03:48Then, 5.
03:48Then, 6.
03:49Then, 1 process.
03:50But, since I am going to traverse my level-order notes
03:54So, we are using level-order traversal.
03:57When there is 3 process,
03:58the horizontal distance will be equal to 3.
04:00Then, 3 will also check that 1 already exists in my map.
04:04It does not exist.
04:05So, what will I do in that case?
04:07I will store 1 in the map.
04:09And, I will store my node 3.
04:12Then, I will come to my third level.
04:14Third level is this level.
04:16When I am going to node 4,
04:17I will have horizontal distance to this 4.
04:21I will calculate horizontal distance which is equal to 0.
04:23As the 0 is calculated,
04:25then, 4 will also check that the map exists in the map.
04:27So, you will see that in the map,
04:290 already exists in the map.
04:31This means that this is my 4.
04:33It is not unique value.
04:34It is not unique value.
04:35So, it does not need to print it.
04:37It means that this 4 is not the top view of the binary tree.
04:40It will not be the top view of the binary tree.
04:41The top view of the binary tree will be the only nodes.
04:44The only nodes will be the first time
04:45which will be the horizontal distance with your horizontal distance.
04:47So, I will add a process.
04:48And, this will be the process by adding this map.
04:51So, it will be 4 not even.
04:52Now, I will come to our next level.
04:54Next level is 5.
04:56The horizontal distance will become 1 or equal.
04:58Then, 5 will check that 1 already exists in the map.
05:02This is 1 already exists in the map.
05:04And, this is the node value for its value.
05:06That is equal to 3.
05:08I will not include the top view of the 5.
05:10because 5 is not the first node for which the horizontal distance is 1
05:13already exists on this node exists on this node already exists on this node 3
05:17which is already the distance has come
05:19which nodes exist in the map already exist on this node
05:24because on those nodes directly on this node
05:27there is another node that is in the top view
05:29so if there is a lens on this node then it will not see the nodes on this node
05:34after this we will go to the next level
05:35meaning in node 6
05:36for this we will calculate horizontal distance
05:37now we will check that 2 exists in the map
05:41already exists in the map
05:44so we will add 2
05:45i have added 2
05:47and also store node 6
05:49now i have processed all the nodes
05:52all the nodes
05:52what did i do with it
05:55i had to track min-max
05:57so min-max
05:58when we reach 2
06:00we will change minimum value
06:02change and minus 1
06:04equal
06:05and here
06:05when we reach 6
06:06when we reach maximum value
06:081
06:08then change and 2
06:10equal
06:11so in last
06:11i will have minimum
06:12minus 1
06:13and maximum
06:142
06:14and as i will travel
06:16i will have all the nodes
06:18all the nodes
06:19store
06:19where?
06:20in the map
06:20in the map
06:22all the nodes
06:23all the nodes
06:25store
06:26then i have to do a loop
06:29loop
06:30loop
06:30loop
06:30from
06:32my minimum
06:33to my maximum
06:35why?
06:36because
06:36minimum
06:36horizontal distance
06:37maximum distance
06:38and i have to know
06:40distance
06:40by 1
06:41increase
06:41every time
06:42i will search for
06:43the map
06:44i will search for
06:46minus 1
06:47.get
06:48which value
06:50returns
06:50then
06:510
06:52then
06:53i will call
06:541
06:54then
06:552
06:56then
06:56i will call
06:58maximum
06:58loop
06:59then
07:00i will call
07:01all the
07:01horizontal distance
07:02i will call
07:03the map
07:03.get
07:05and
07:05what will
07:06value
07:07return
07:08first
07:08i will print
07:112
07:11then
07:12i will print
07:122
07:13then
07:14i will print
07:151
07:15then
07:15i will print
07:203
07:20then
07:21call
07:212
07:21i will print
07:226
07:23and
07:24first
07:27we will
07:30see
07:31node
07:311
07:31then
07:32node
07:323
07:33then
07:33node
07:346
07:34this
07:36is
07:36my
07:36top
07:36view
07:37of the binary tree
07:38so
07:38i have
07:38used
07:39two
07:39things
07:39one
07:40one
07:41one
07:41one
07:42one
07:44one
07:45one
07:45one
07:45that
07:45i will
07:47calculate
07:48the
07:51i have used
07:52the maps
07:53property
07:53i use
07:55the
07:57unique
07:57keys
07:58and
07:58there
08:02i will
08:04what
08:04the
08:05horizontal
08:05distance
08:05is
08:06not
08:06map exist or not. Now this is the question, if we are first time top view, in fact if we
08:12are
08:12first time binary trees, then it will be intimidating because we have taken a big explanation,
08:18I have included a new data structure, horizontal distance also put a new property. And
08:24with the explanation, maybe it will not be clear. But if the horizontal distance property is clear,
08:30the first level order which we have learned, it was good to understand. And the map, which I have told,
08:37we are clear. So then understanding code, we will know that the top view of a binary tree
08:41is calculated. So now we have to stay with patience. First of all, we have to see code.
08:46We will see code once again. After that, we will see the approach again.
08:51After seeing code, we will see the approach again, so we will get more intuitively to understand.
08:56So don't worry if one or two parts are not clear. I will tell you the whole level order traversal
09:00I will tell you the whole thing. And after that, we will get more clarity,
09:04how to solve this question.