Last active
January 17, 2022 16:47
-
-
Save CollinShoop/c13ba9c544264086d224be0fc0691e67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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