Tree_Practice_Questions_with_Solutions
Tree_Practice_Questions_with_Solutions
Q1. Write an algorithm or recursive function to perform Inorder traversal of a binary tree. Dry-run it
Ans: Algorithm:
2. Visit root
Q2. Construct the Binary Tree from Inorder: D B E A F C and Preorder: A B D E C F. Draw the final tr
Final Tree:
/\
B C
/\ /
D EF
Q3. Write an algorithm for Level-Order Traversal using a queue. State its time and space complexity
Ans: Algorithm:
3. Repeat
Q4. Write a recursive function to count the number of leaf nodes in a binary tree.
Tree & AVL Tree Practice Questions with Solutions
Q6. Define Full, Complete, Perfect, and Skewed Binary Trees with examples.
Q7. Insert 40, 20, 60, 10, 30, 50, 70 into BST and draw final tree.
40
/ \
20 60
/\ /\
10 30 50 70
Ans: Find inorder successor (50). Replace 40 with 50 and delete original 50.
Q9. Construct AVL Tree by inserting: 9, 5, 10, 0, 6, 11, -1. Show all rotations.
Ans: Apply insertions. LR rotation at 5 needed. Final root = 9. Show stepwise tree.
Q10. What is balance factor in AVL? Show LR Rotation on inserting 50, 40, 45.
Ans: Balance Factor = height(left) - height(right). Insert -> 50, 40, 45 causes LR. First left rotate on 40's child,
2. Databases (B-trees)
3. Routing (Tries)
Ans: Tree:
/\
+ -
/\/\
a bc d
Tree & AVL Tree Practice Questions with Solutions
Prefix: * + a b - c d
Postfix: a b + c d - *
Ans: Use stack. Push left till NULL, pop, visit, move right. Repeat.
Q15. Write 4-point differences: BST vs AVL, Level vs Inorder, Stack vs Queue (in context of trees).