Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5765 |

@blackshadows | 5715 |

@akhayrutdinov | 5111 |

@mb1973 | 4993 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 1272 |

@bt8816103042 | 739 |

@rohitanand | 495 |

@codeantik | 479 |

@shalinibhataniya1097 | 472 |

@amrutakashikar2 | 464 |

@thealchemist627 | 456 |

@dalwainazism125 | 453 |

@shivanandp12345678910 | 453 |

@akashkale117 | 453 |

@SherlockHolmes3 | 447 |

Complete Leaderboard |

Given a Binary Tree, find the vertical traversal of it starting from the leftmost level to the rightmost level.

If there are multiple nodes passing through a vertical line, then they should be printed as they appear in **level order** traversal of the tree.

**Example 1:**

**Input:
**** **2
\
3
/
4
**Output: **2 4 3**
**

**Example 2:**

**Input:
**** **1
/ \
2 3
/ \ \
4 5 6
**Output: **4 2 1 5 3 6**
**

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **verticalOrder() **which takes the root node as input and returns an array containing the vertical order traversal of the tree from the leftmost to the rightmost level. If 2 nodes lie in the same vertical level, they should be printed in the order they appear in the level order traversal of the tree.

**Expected Time Complexity: **O(N).

**Expected Auxiliary Space: **O(N).

**Constraints:**

1 <= Number of nodes <= 5000

**Note: **The **Input/Ouput** format and **Example **given are used for system's internal purpose, and should be used by a user for **Expected Output **only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Login 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?

Yes
Vertical Traversal of Binary Tree

...