14 Stack Permutations 13-01-2025
14 Stack Permutations 13-01-2025
VIEWS OF TREE
EXPLANATION
aspects of the tree's structure or content, such as nodes visible from the
top, bottom, left, or right. These views help analyze and understand the
EXPLANATION
❖ Horizontal View
❖ Vertical View
❖ Left View
❖ Right View
❖ Top View
❖ Bottom View
VIEWS OF TREE
Horizontal View:
The horizontal view of a binary tree shows the nodes from left to right at each
level. Nodes at the same level are listed in the order of their appearance, from
left to right.
Example:
A
/ \
B C
/ \ / \
D E F G
Horizontal View:A B C D E F G
VIEWS OF TREE
A
/ \
B C
/ / \
D E F
Vertical View:
D B A E C F
VIEWS OF TREE
The Left View provides a view of the nodes seen from the left side of the tree.
It includes the leftmost node at each level.
Example:
A
/ \
B C
/ \ / \
D E F G
Left View: A B D
VIEWS OF TREE
The Right View offers a view of the nodes seen from the right side of the tree.
It includes the rightmost node at each level.
Example:
A
/ \
B C
/ \ / \
D E F G
Right View: A C G
VIEWS OF TREE
The Top View displays nodes visible from the top of the tree when viewed from
above.
It shows the outermost nodes at each vertical level.
Example:
A
/ \
B C
/ / \
D E F
Top View: D B A C F
VIEWS OF TREE
import java.util.*;
class TreeNode { public class Main {
char val; // Function to obtain the Top View
TreeNode left; of a binary tree
TreeNode right; public static List<Character>
public TreeNode(char val) { topView(TreeNode root) {
this.val = val; List<Character> topView = new
left = null; ArrayList<>();
right = null; if (root == null) {
} return topView;
} }
class Pair { Map<Integer, Character>
TreeNode node; verticalMap = new TreeMap<>();
int hd; Queue<Pair> queue = new
public Pair(TreeNode node, int hd) LinkedList<>();
{ queue.offer(new Pair(root, 0));
this.node = node; while (!queue.isEmpty()) {
this.hd = hd; Pair pair = queue.poll();
} TreeNode node = pair.node;
}
VIEWS OF TREE
A
/ \
B C
/ / \
D E F
\
G
VIEWS OF TREE
the tree.
INTERVIEW QUESTIONS
level when viewed from below the tree, including the nodes that
3.In the "Top View" of a Binary Tree, what does the outermost node
at each vertical level represent?
nodes visible from the top when viewed from above the tree.
INTERVIEW QUESTIONS
bottom.
/ Ethnus /ethnus /
ethnuscodemith Codemithra code_mithra
ra
https://
learn.codemithra.com