Programming interview questions and answers

Level order traversal of a binary search tree with a new line after each level

Write a program to do level order printing of a binary search tree with a new line after each level The approach:- As in the regular level order traversal we need a queue. Push the root node into the queue and set the current level as 1. When queue not empty dequeue a node and …

Level order traversal of a binary search tree with a new line after each level Read More »

Zig zag traversal of binary search tree

Write a program to do zig zag traversal of a binary search tree The approach:- Need assistance of a queue. Same as level order traversal except that instead of pushing the left sub-tree into the queue first push the right tree sub-tree. C++ program to do zig zag traversal of a binary search tree #include <iostream>#include …

Zig zag traversal of binary search tree Read More »

Find the minimum depth of binary search tree and maximum depth of binary search tree

Write a program to find the minimum depth of binary search tree and maximum depth of binary search tree The approach:- Depth is the height of a leaf node starting from the root. Easiest approach is to have recursive function which finds the depth of left and write sub trees. C++ program to find the minimum …

Find the minimum depth of binary search tree and maximum depth of binary search tree Read More »

Find least common ancestor (lca) of 2 nodes in a binary search tree

Write a program to find least common ancestor (lca) of 2 nodes in a binary search tree The approach:- Starting from the root node. Check if the node key is between the two provided keys. If so, the current node is the least common ancestor. If the two provided keys are greater than the current …

Find least common ancestor (lca) of 2 nodes in a binary search tree Read More »