-
-
Save InterviewBytes/e08b5174f6f2c147d58090e1c5cbf4e4 to your computer and use it in GitHub Desktop.
Bottom Left item value in a binary tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.interviewbytes.trees; | |
import java.util.Deque; | |
import java.util.LinkedList; | |
public class BottomLeft { | |
public int findBottomLeftValue(TreeNode root) { | |
Deque<TreeNode> deque = new LinkedList<>(); | |
deque.offer(root); | |
int leftBottom = -1; | |
while (!deque.isEmpty()) { | |
leftBottom = deque.peekFirst().val; | |
int size = deque.size(); | |
while (size > 0) { | |
TreeNode node = deque.poll(); | |
if (node.left != null) deque.offer(node.left); | |
if (node.right != null) deque.offer(node.right); | |
size--; | |
} | |
} | |
return leftBottom; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.interviewbytes.trees; | |
public class TreeNode { | |
int val; | |
TreeNode left; | |
TreeNode right; | |
TreeNode(int x) { | |
val = x; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment