513. 找树左下角的值

2019-01-24  本文已影响8人  小王同学加油

find-bottom-left-tree-value

给定一个二叉树,在树的最后一行找到最左边的值。

阅读

  1. 不懂最左值是什么意思?
如果保证获取的是5呢

中顺第一个大于上层的元素就是当前最大层最左的元素

最左-->中顺遍历第一个元素 -->同层次中序遍历第一元素

func findBottomLeftValue(root *TreeNode) int {
    maxLevle := 0
    leftValue := 0
    findBottomValue(root, 1, &maxLevle, &leftValue)
    return leftValue

}

func findBottomValue(root *TreeNode, level int, maxLevle *int, leftValue *int) {
    if root == nil {
        return
    }
    findBottomValue(root.Left, level+1, maxLevle, leftValue)
    
    if level > *maxLevle {
        *maxLevle = level
        *leftValue = root.Val
    }
    findBottomValue(root.Right, level+1, maxLevle, leftValue)
}

Time Complexity : O(n)

Q1 一般性能优化都是o(n2) ->o(n)->0(logn) 能不能在优化

 时间复杂度 o(n)
image.png

参考:
https://blog.csdn.net/fangjian1204/article/details/39179343
https://blog.csdn.net/u013904605/article/details/44596743?utm_source=blogxgwz0

题目


image.png
上一篇 下一篇

猜你喜欢

热点阅读