📜  二叉树最后一级的左节点 - 代码示例

📅  最后修改于: 2022-03-11 14:57:34.589000             🧑  作者: Mango

代码示例1
# left node of the last level in a binary tree
# doing a dfs and maintaining the levels as i proceed
def findBottomLeftValue(self, root):
    def helper(data):
        depth, node = data
        left = helper([depth+1, node.left]) if node.left else [depth, node.val]
        right = helper([depth+1, node.right]
                       ) if node.right else [depth, node.val]
        if left[0] == right[0]:
            return left
        return max(left, right)

    return helper((0, root))[1]