What is ZigZag level order traversal?
A better idea for ZigZag Level Order Traversal is the Breadth-First Approach that we use in a Level Order Traversal. Add a root node to Queue. Iterate until the contents of the queue become empty. Reset the level list on each level to a new array. On each level, pop the last visited element from the Queue.
How do you traverse a tree in ZigZag order?
Zig Zag Level order traversal of a tree using single array
- Insert the root element into the queue and again insert NULL into the queue.
- For every element in the queue insert its child nodes.
- If a NULL is encountered then check the direction to traverse the particular level is left to right or right to left.
How do you find the top view of a binary tree?
Algorithm
- Create a map to store the top-view of the binary tree.
- Perform inorder traversal of the binary tree.
- During traversal keep track of vertical height(h) and horizontal width(w) of each of the tree nodes.
- For the node being visited currently, check if it’s horizontal width level has been visited or not.
What is lowest common ancestor in binary tree?
The lowest common ancestor (LCA) of two nodes x and y in a binary tree is the lowest (i.e., deepest) node that has both x and y as descendants, where each node can be a descendant of itself (so if x is reachable from w , w is the LCA).
What is diameter of binary tree?
The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root .
How do you print nodes from a binary tree?
You start traversing from the root, then go to the left node, then you again go to the left node until you reach a leaf node. At that point in time, you print the value of the node or mark it as visited and move to the right subtree. Continue the same algorithm until all nodes of the binary tree are visited.