0
点赞
收藏
分享

微信扫一扫

二叉树左下角值/路径总和

搬砖的小木匠 2022-05-01 阅读 109

513.找树左下角的值

在这里插入图片描述
那么如果找最左边的呢?可以使用前序遍历,这样才先优先左边搜索,然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        que = deque([root])
        res = 0
        while que:
            size = len(que)
            for i in range (size):
                # 每一层都记录第一个结点的值
                if i == 0:
                    res = que[i].val
                cur = que.popleft()
                if cur.left:
                    que.append(cur.left)
                if cur.right:
                    que.append(cur.right)
        return res

                

class Solution:
    def findBottomLeftValue(self, root: TreeNode) -> int:
        max_depth = -float("INF")
        leftmost_val = 0

        def __traverse(root, cur_depth): 
            nonlocal max_depth, leftmost_val
            if not root.left and not root.right: 
                if cur_depth > max_depth: 
                    max_depth = cur_depth
                    leftmost_val = root.val  
            if root.left: 
                cur_depth += 1
                __traverse(root.left, cur_depth)
                cur_depth -= 1
            if root.right: 
                cur_depth += 1
                __traverse(root.right, cur_depth)
                cur_depth -= 1

        __traverse(root, 0)
        return leftmost_val

112. 路径总和

在这里插入图片描述

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def hasPathSum(self, root, targetSum):
        """
        :type root: TreeNode
        :type targetSum: int
        :rtype: bool
        """
        def traversal(root, count):
            # 如果count为0 且为叶子节点 说明到尽头 找到了路径返回真
            # 中 处理单层逻辑
            if not root.left and not root.right and count == 0:
                return True 
            # 叶子节点 说明到尽头 假
            if not root.left and not root.right:
                return False
            
            # 左 
            if root.left:
                count -= root.left.val
                if traversal(root.left, count):return True #递归 处理左节点
                count += root.left.val #回溯
    
            # 右
            if root.right:
                count -= root.right.val
                if traversal(root.right, count):return True
                count += root.right.val#回溯
            
            return False

        if root == None:
            return False
        return traversal(root, targetSum - root.val)

113. 路径总和ii

在这里插入图片描述113.路径总和ii要遍历整个树,找到所有路径,所以递归函数不要返回值!

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def pathSum(self, root, targetSum):
        """
        :type root: TreeNode
        :type targetSum: int
        :rtype: List[List[int]]
        """

        def traversal(root, count):
            # 如果count为0 且为叶子节点 说明到尽头 找到了路径 添加
            # 中 处理单层逻辑
            if not root.left and not root.right and count == 0:
                result.append(path[:])# 注意这里不能写成result.append(path)
                return
            
            # 叶子节点 说明到尽头 直接返回
            if not root.left and not root.right:
                return 
            
            # 左 
            if root.left:
                path.append(root.left.val)
                count -= root.left.val
                traversal(root.left, count)
                count += root.left.val #回溯
                path.pop()#回溯

    
            # 右
            if root.right:
                path.append(root.right.val)
                count -= root.right.val
                traversal(root.right, count)
                count += root.right.val#回溯
                path.pop()#回溯
            
           

        result = []
        path = []
        if not root:
            return []
        path.append(root.val)
        traversal(root, targetSum -root.val)
        return result


举报

相关推荐

0 条评论