513 find bottom left tree value

·data-structure-and-algorithm
#binary-tree

513. 找树左下角的值

go:

/**

 * Definition for a binary tree node.

 * type TreeNode struct {

 *     Val int

 *     Left *TreeNode

 *     Right *TreeNode

 * }
 */
func findBottomLeftValue(root *TreeNode) int {
    if root == nil {
        return 0
    }

    var v int
    queue := []*TreeNode{root}

    for len(queue) > 0 {
        levelSize := len(queue)

        for i := 0; i < levelSize; i++ {
            node := queue[0]
            queue = queue[1 :]

            if i == 0 {
                v = node.Val
            }

            if node.Left != nil {
                queue = append(queue, node.Left)
            }

            if node.Right != nil {
                queue = append(queue, node.Right)
            }
        }
    }

    return v
}