Leetcode DAY 18:找树左下角的值 and 路径总和 and 从中序与后序遍历序列构造二叉树 and 从前序与中序遍历序列构造二叉树

513.找树左下角的值

(迭代)

class Solution:
    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        que = deque([root])
        while que:
            size = len(que)
            for i in range (size):
                node = que.popleft()
                # 记录每行第一个数 每次循环被覆盖 最后res为最后一行第一个数
                if i == 0:
                    res = node.val
                if node.left:
                    que.append(node.left)
                if node.right:
                    que.append(node.right)
        return res

层序遍历的方法 记录每层第一个数 并更新 最后的res就是最下层第一个数

112.路径总和

class Solution:
    def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        if not root:
            return False
        sum = 0
        res = []
        self.Traversal(root, sum, res)
        if targetSum in res:
            return True
        else:
            return False        

    def Traversal(self, cur: Optional[TreeNode], sum: int, res: List[int]): 
        sum += cur.val
        if cur.left == None and cur.right == None:
            res.append(sum)
        if cur.left:
            self.Traversal(cur.left, sum, res)
        if cur.right:
            self.Traversal(cur.right, sum, res)

113.路径总和II

class Solution:
    def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:

        def Traversal(cur: Optional[TreeNode], count: int):
            # 递归结束条件:走到叶子节点了
            if cur.left == None and cur.right == None:
                # 满足targetsum的条件
                if count == 0:
                    res.append(path[:])
                return
                             
            if cur.left:
                path.append(cur.left.val)
                Traversal(cur.left, count - cur.left.val)
                #回溯
                path.pop()
            if cur.right:
                path.append(cur.right.val)
                Traversal(cur.right, count - cur.right.val)
                #回溯
                path.pop()
                
        if not root: return []
        res = []
        path = []
        # 先把根节点放到路径里
        path.append(root.val)
        Traversal(root, targetSum - root.val)
        return res

106.从中序与后序遍历序列构造二叉树

class Solution:
    def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
        # 处理树为空的情况
        if not postorder:
            return None
        # 找到根节点值 建立根节点
        root_val = postorder[-1]
        root = TreeNode(root_val)
        # 找到分割点
        seperated_index = inorder.index(root_val)
        # 分割出左、右子树的中序和后序
        left_inorder = inorder[:seperated_index]
        right_inorder = inorder[seperated_index + 1:]
        left_postorder = postorder[:len(left_inorder)]
        right_postorder = postorder[len(left_inorder): len(postorder) - 1] 
        # 递归
        root.left = self.buildTree(left_inorder, left_postorder)
        root.right = self.buildTree(right_inorder, right_postorder)
        return root

重要思路是:从前序或后序遍历中找到根节点,根据根节点对前序遍历和后序遍历的左右子树进行分割,分割出 左/右后序 左/右中序  然后进行递归   

105.从前序与后序遍历序列构造二叉树

class Solution:
    def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
        if not preorder:
            return None

        root_val = preorder[0]
        root = TreeNode(root_val)
        sep_idx = inorder.index(root_val)

        left_inorder = inorder[: sep_idx]
        right_inorder = inorder[sep_idx + 1:]
        left_preorder = preorder[1: 1 + len(left_inorder)]
        right_preorder = preorder[1 + len(left_inorder):]

        root.left = self.buildTree(left_preorder, left_inorder)
        root.right = self. buildTree(right_preorder, right_inorder)
        return root

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值