Java straightforward solution


  • 0
    J
    public class Solution {
        TreeNode target;
        int maxDepth = 0, leftMost = 0;
        
        public int findBottomLeftValue(TreeNode root) {
            target = root;
            get(root, 0, 0);
            return target.val;
        }
        
        private void get(TreeNode root, int depth, int col) {
            if (root == null) return;
            if (depth > maxDepth) {
                maxDepth = depth;
                target = root;
            } else if (depth == maxDepth) {
                if (leftMost > col) {
                    leftMost = col;
                    target = root;
                }
            }
            get(root.left, depth + 1, col - 1);
            get(root.right, depth + 1, col + 1);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.