Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Find the Maximum Depth OR Height of a Binary Tree

Objec­tive: Given a binary tree, find the height of it

Input: A Binary Tree

Out­put: Height of a binary tree

Exam­ple:

Tree Height - Example

Tree Height — Example

Approach:

       Recursion:

  • Get the height of left sub tree, say leftHeight
  • Get the height of right sub tree, say rightHeight
  • Take the Max(leftHeight, rightHeight) and add 1 for the root and return
  • Call recur­sively.

Time Com­plex­ity : O(n)

get Height of a tree - Recursion

get Height of a tree — Recursion

Com­plete Code:


Out­put:

Height of the Tree is 7

You may also like...

  • Anuj

    Expla­na­tion with dia­gram is just amaz­ing! Thanks!

    • tuto­ri­al­hori­zon

      Thanks Anuj