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 Deepest Node in a Binary Tree.

Objec­tive: - Given a binary tree, Find the deep­est node in it.

Approach:

  • Take two global vari­able as “deep­estlevel” and “value”.
  • start­ing with level=0, Do the inorder tra­ver­sal and when­ever you go down one level ( root.left OR root.right), increase the level by 1.
  • Keep check­ing if deep­estlevel < level, if yes then update the “deep­estlevel ” and “value “.
  • At the end return “value”, which will the deep­est node value.
  • See the code for bet­ter explanation.

Code:


Out­put:

Deepest child is: 8

You may also like...

  • Lance

    When call­ing for the right child, shouldn’t it be find (root.right, ++level) instead of find(root.right, level)?

    • tuto­ri­al­hori­zon

      If u notice when we are call­ing find(root.left, ++level) we have already increased the level value. So when we make a call to root.right, it will be called with incre­mented value. To avoid con­fu­sion u can call find (root.left, level+1) and then find(root.right, level+1)

      • Lance

        You’re right. Thanks

        • tuto­ri­al­hori­zon

          You r welcome !