Skip to content

Instantly share code, notes, and snippets.

@InterviewBytes InterviewBytes/BottomLeft.java Secret
Created Jun 12, 2017

Embed
What would you like to do?
Bottom Left item value in a binary tree
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;
}
}
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
You can’t perform that action at this time.