clueless coding // TODO: be smarter

LeetCode Find Bottom Left Tree Value: Java Solution



TL;DR Code Solution


public int findBottomLeftValue(TreeNode root) {

    Queue<TreeNode> queue = new LinkedList<TreeNode>();
    queue.offer(root);
    TreeNode candidate = null;
    while (!queue.isEmpty()) {

        int size = queue.size();

        for (int i = 0; i < size; i++) {
            if (i == 0) {
                candidate = queue.peek();
            }
            TreeNode curr = queue.poll();
            if (curr.left != null) queue.offer(curr.left);
            if (curr.right != null) queue.offer(curr.right);
        }


    }

    return candidate.val;

}