Be the first user to complete this post
|
Add to List |
Find the Deepest Left Node in a Binary Tree.
Objective: - Given a binary tree, Find the deepest left node in it.
Approach:
- This approach is very similar to "Find the Deepest Node in a Binary Tree" with little modification.
- Take two global variables as "deepestlevel" and " deepLeftNode".
- 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 the current node is the left child and deepestlevel < level, if yes then update the "deepestlevel " and " deepLeftNode ".
- At the end return " deepLeftNode ", which will be the deepest node value.
- See the code for a better explanation.
Code:
https://gist.github.com/thmain/6878f09ceda9f7de5bf1
Output:
Deapest Left child is: 4
Approach: - Without using global variables
We will keep track of the deepest left node and pass it along during recursion. Will take the Result object ( with data and level). See the code below for more understanding.
Code:
Output:
Deapest Left child is: 18
Also Read: