12345678910111213141516171819202122232425262728293031323334 |
- /**
- * Definition for a binary tree node.
- * type TreeNode struct {
- * Val int
- * Left *TreeNode
- * Right *TreeNode
- * }
- */
- func findBottomLeftValue(root *TreeNode) int {
- if root == nil {
- return -1
- }
- queue := make([]*TreeNode, 0)
- queue = append(queue, root)
- for len(queue) != 0 {
- bl := queue[0].Val
- nextLV := make([]*TreeNode, 0)
- for len(queue) != 0 {
- head := queue[0]
- queue = queue[1:]
- if head.Left != nil {
- nextLV = append(nextLV, head.Left)
- }
- if head.Right != nil {
- nextLV = append(nextLV, head.Right)
- }
- }
- if len(nextLV) == 0 {
- return bl
- }
- queue = nextLV
- }
- return -1
- }
|