# Find the Deepest Node in a Binary Tree.

Objective: – Given a binary tree, write an algorithm to Find the deepest node in it.

Approach:

• Take two global variable as “deepestlevel” and “value“.
• starting with level=0, Do the inorder traversal and whenever you go down one level ( root.left OR root.right), increase the level by 1.
• Keep checking if deepestlevel < level, if yes then update the “deepestlevel ” and “value “.
• At the end return “value“, which will the deepest node value.
• See the code for better explanation.

Code:

Output:

```Deepest child is: 8
```

### 5 Responses

1. Lance says:

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

• tutorialhorizon says:

If u notice when we are calling 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 incremented value. To avoid confusion u can call find (root.left, level+1) and then find(root.right, level+1)

• Lance says:

You’re right. Thanks

• tutorialhorizon says:

You r welcome !

• Martin says:

Agree, this looks a bit confusing. I would rather do find(root.left, level + 1) and find(root.right, level +1)

This site uses Akismet to reduce spam. Learn how your comment data is processed.