Skip to content

Instantly share code, notes, and snippets.

@CollinShoop
Last active January 17, 2022 16:47
Show Gist options
  • Save CollinShoop/c13ba9c544264086d224be0fc0691e67 to your computer and use it in GitHub Desktop.
Save CollinShoop/c13ba9c544264086d224be0fc0691e67 to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func findBottomLeftValue(root *TreeNode) int {
var find func(node *TreeNode, depth int) (int, int)
find = func(node *TreeNode, depth int) (int, int) {
if node == nil {
return -1, -1
}
// leaf
if node.Left == nil && node.Right == nil {
return node.Val, depth
}
// find answer left
left, leftDepth := find(node.Left, depth+1)
// find answer right
right, rightDepth := find(node.Right, depth+1)
// prefer left
if leftDepth >= rightDepth {
return left, leftDepth
}
return right, rightDepth
}
v, _ := find(root, 0)
return v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment