Vertical Order traversal in binary tree
Vertical Order

Vertical Order traversal in binary tree

We generally talk about Inorder, PreOrder, PostOrder and LevelOrder traversals in binary tree, but we generally do not use verticalOrder traversal in DSA, let us look into this traversal.

Vertical order traversal is when we traverse the tree level wise and the levels are considered in vertical direction with the mean position being the center or the root TreeNode.

Let us quickly jump into it's algorithm -

This is the tree we are considering -


Binary tree

The Vertical Order Traversal -


VOT code

Let us see how can we manipulate the results fetched from the vot function for a better clarity -

Manipulating the result

In this algorithm we keep the distance of root as zero and push the pair of root and distance into a queue. Then we explore the queue's first element, push the left child with a distance one less than the parent's distance and for the right child we push the TreeNode with one more than the parent's distance.

Finally to manipulate the results and show it systematically, by getting the minLevel and maxLevel and iterating over the map where we kept the TreeNodes corresponding to each level.

Finally let us check the result in console -


Result

Happy coding, I keep sharing such content on a regular basis. Do follow me for more such content.


要查看或添加评论,请登录

Siddhant Mishra的更多文章

  • SuspendCoroutine

    SuspendCoroutine

    Today I will be writing about the usage of an important concept in Android namely suspendCoroutine. By definition…

  • findViewById - Internals

    findViewById - Internals

    Hope everyone is doing well. Today I will be writing about the internal working of findViewById in Android.

    2 条评论
  • Lambda functions

    Lambda functions

    Today I will be writing about two different yet intriguing way to write the lambda functions in Kotlin. Conventionally…

    1 条评论
  • Exceptions in Coroutines

    Exceptions in Coroutines

    Hi everyone, today I will be writing about exceptions in Coroutine and how to handle them gracefully. First let us see…

  • Trie data structure

    Trie data structure

    Today I will write about a magical data structure namely Trie. When we talk of storing and retriving some information…

  • LCA in Binary Tree

    LCA in Binary Tree

    Today I will be writing about the LCA(Least Common Ancestor) for a binary tree. LCA refers to the node in the binary…

  • Delegates in Kotlin

    Delegates in Kotlin

    Today I will be writing about my understanding regarding the delegates in Kotlin. The english meaning of Delegation…

  • TypeAlias in Kotlin

    TypeAlias in Kotlin

    Today I will be writing about TypeAlias in Kotlin and how do I understand and use it in my day to day coding. Typealias…

  • Reified in Kotlin

    Reified in Kotlin

    Today I will be writing about the type erasure in Java and Kotlin and its solution. The concept of type erasure stems…

    1 条评论
  • Jetpack Compose - Interesting features to dive deeper into.

    Jetpack Compose - Interesting features to dive deeper into.

    Recently I got to migrate my sample project from the conventional XML to Jetpack Compose, and I would like to list down…

    3 条评论

社区洞察