Technical Questions‎ > ‎DS‎ > ‎Tree‎ > ‎

Solution

1. The binary tree is given. Print row elements in zig-zag order.
               1
        2            3
    4        5    6    7
 
    Print: 1, 3, 2, 4, 5, 6, 7
 
    Solution 1:
    Keep Stack S1, S2
    Push odd row in S1 as right first and then left child
    Read and print from stack.
    Push even row in S2 as left first and then right child
    Read and print from stack.
 
    S1    S2    Print
    1
                    1
            3
            2
                    1 3 2
    4
    5
    6
    7
                    1 3 2 4 5 6 7
 
2.

3. 

4. Print the path from root to a particular node given as input in a binary tree in the sequential order.

(need not be BST or balanced).


    Solution 1: O(h) , h is height of tree

    Start a recursive tree traversal.

    Once node is found return true, Check for true in stack unwind and [print left\right or store bit 0\1 in vector and print at the end.]

    If you reach leaf, return false.

Comments