Boundary Traversal of binary tree
Medium Accuracy: 26.78% Submissions: 56976 Points: 4

Write a function to print the Boundary Traversal of a binary tree. Boundary Traversal of a binary tree here means that left boundary nodes, leaves, and right boundary in order without duplicate nodes.  (The values of the nodes may still be duplicates.)

Note: Left boundary is defined as the path from the root to the left-most node. The right boundary is defined as the path from the root to the right-most node. If the root doesn't have a left subtree or right subtree, then the root itself is left boundary or right boundary. Note this definition only applies to the input binary tree, and not to any subtrees.
For the below tree, the function should print 20 8 4 10 14 25 22 .













Example 1:

      /   \
     2    3       
Output: 1 2 3

The first test case represents a tree
with 3 nodes and 2 edges where the
root is 1, the left child of 1 is 2
and the right child of 1 is 3. And
boundary traversal of this tree
prints nodes as 1 2 3.

Example 2:

        /   \
       20   30
     /   \
    40   60
Output: 10 20 40 60 30

Your Task:
This is a function problem. You don't have to take input. Just complete the function printBoundary() that takes the root node as input and returns an array containing the boundary values in anti-clockwise.

Expected Time Complexity: O(N). 
Expected Auxiliary Space: O(Height of the Tree).

1 ≤ Number of nodes ≤ 105
1 ≤ Data of a node ≤ 105

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Output Window