Be the first user to complete this post
|
Add to List |
142. Locate the Farthest Left Node on the Bottom Level
Objective: - Given a binary tree, Find the deepest left node in it.
Approach:
- This approach is 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.
Output:
Deepest 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.
Output:
Deepest Left child is: 18