Top View and Bottom View of Binary tree | Iterative Method | Trees

preview_player
Показать описание
In this video, I have discussed how to find top view and bottom view of a binary tree. This can be done both recursively and iteratively. Here, I have discussed the iterative approach.
Logic: Level order traversal is done and we use a map in which the key is horizontal distance from root and the value is {node, level}. If one node with the same horizontal distance comes again, we check if level of new node is lower or higher than the one stored in the map. If level of new node is lower, then we replace it.

Note: If there are multiple top-most/bottom-most nodes at same level and same horizontal distance, then print the later one in level order traversal.

00:00 Introduction
00:16 What is Top View and Bottom View
01:24 Top View Pseudo Code
11:08 Bottom View Pseudo Code
12:34 Implementation

-------------------------------------------------------------
I live in New Delhi and love explaining programming concepts. I have done M.Tech(BITS Pilani) + B.Tech(PEC, Chandigarh) in Computer Science and am currently working as a software engineer in a MNC.
If you like my content, please like, share my videos and subscribe to the channel.
-------------------------------------------------------------

For other tree tutorials, please refer to the below links:

For in-depth Graph theory and implementation details, please refer to the below playlist:

#DataStructure,#Trees,#FitCoder,#Algorithm,#competitiveprogramming,#binarytree
Рекомендации по теме
Комментарии
Автор

00:00 Introduction
00:16 What is Top View and Bottom View
01:24 Top View Pseudo Code
11:08 Bottom View Pseudo Code
12:34 Implementation

FitCoder
Автор

Amazingly Explained, although I know the way but you clears my doubt

arpitsaini
Автор

thank you, it was a very nice explanation.

manavshah
Автор

please make videos on interviews process

ayushnamdev
Автор

The solution may be incorrect for the following tree:

root = Node(0)
root.left = Node(1)
root.right = Node(2)
root.left.right = Node(3)
root.right.left = Node(4)

Actual output: 1 4 5
Expected Output: 1 3 4 5

Output is incorrect because the distance of {node, node.left.right, node.right.left} for any node is the same. So, both node.left.right and node.right.left should be added to the output, instead of just selecting node.right.left.

shailshah
Автор

Sir please explain the tc for both recursive and iterative approach in brief.

aakashgoswami
Автор

Amazing Sirrr.. Could you please tell do i have to study Xor tree and all for campus placemnts and all? Or the topics covered by you will be suffiecnt I have practised them on leetcode

abhigyansharma