/*
* @lc app=leetcode.cn id=513 lang=typescript
*
* [513] 找树左下角的值
*/
// @lc code=start
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function findBottomLeftValue(root: TreeNode | null): number {}
// @lc code=end
function findBottomLeftValue(root: TreeNode | null): number {
if (!root) return -1
let res = 0,
left = Infinity,
bottom = -Infinity
const dfs = (node: TreeNode, l = 0, b = 0) => {
if (bottom === b) {
if (left > l) {
res = node.val
left = l
bottom = b
}
} else if (bottom < b) {
res = node.val
left = l
bottom = b
}
if (node.left) {
dfs(node.left, l - 1, b + 1)
}
if (node.right) {
dfs(node.right, l + 1, b + 1)
}
}
dfs(root)
return res
}