Skip to content

Instantly share code, notes, and snippets.

@InterviewBytes
Created June 12, 2017 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InterviewBytes/e08b5174f6f2c147d58090e1c5cbf4e4 to your computer and use it in GitHub Desktop.
Save InterviewBytes/e08b5174f6f2c147d58090e1c5cbf4e4 to your computer and use it in GitHub Desktop.
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