代码随想录刷题第十八天| 513.找树左下角的值 ● 112. 路径总和 113.路径总和ii ● 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树

代码随想录刷题第十八天

找树左下角的值 (LC 513)

题目思路:

在这里插入图片描述

代码实现:

class Solution(object):
    def findBottomLeftValue(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        res = self.Traversal(root)
        return res[1]
        
    def Traversal (self, root):
        if root is None:
            return [0,0]

        if root.left is None and root.right is None:
            return [1, root.val]

        left = self.Traversal(root.left)
        right = self.Traversal(root.right)       

        current = left if left[0]>=right[0] else right
        current[0]+=1
        return current

路径总和 (LC 112) 搜索单条路径,使用后序遍历立即返回模式

题目思路:

在这里插入图片描述

代码实现:

class Solution(object):
    def hasPathSum(self, root, targetSum):
        """
        :type root: TreeNode
        :type targetSum: int
        :rtype: bool
        """
        if root is None:
            return False
        res = self.traversal(root, targetSum-root.val)
        return res
    
    def traversal (self, node, count):
        # 遇到叶子节点
        if node.left is None and node.right is None:
            if count == 0:
                return True 
            else:
                return False
        
        if node.left is not None:
            count -= node.left.val
            left = self.traversal(node.left,count)
            count += node.left.val
            if left:
                return True
        if node.right is not None:
            count -= node.right.val
            right = self.traversal(node.right, count)
            count += node.right.val
            if right:
                return True
        return False

路径总和 二 (LC 113) 搜索全部路径,使用后序遍历逻辑处理后返回模式

题目思路:

在这里插入图片描述

代码实现:

class Solution(object):
    def pathSum(self, root, targetSum):
        """
        :type root: TreeNode
        :type targetSum: int
        :rtype: List[List[int]]
        """
        if root is None:
            return []
        result = []
        path = [root.val]
        self.traversal(root, path, targetSum-root.val, result)
        return result

    def traversal(self, root, path, count,result):
        if root.left is None and root.right is None:
            if count == 0:
                result.append(path[:])
            return
        
        if root.left is not None:
            count-=root.left.val
            path.append(root.left.val)
            self.traversal(root.left, path, count,result)
            path.pop()
            count+=root.left.val

        if root.right is not None:
            count-=root.right.val
            path.append(root.right.val)
            self.traversal(root.right, path, count,result)
            path.pop()
            count+=root.right.val

从中序与后序遍历序列构造二叉树 (LC 106)

题目思路:

在这里插入图片描述

代码实现:

class Solution(object):
    def buildTree(self, inorder, postorder):
        """
        :type inorder: List[int]
        :type postorder: List[int]
        :rtype: TreeNode
        """
        if len(postorder) == 0:
            return None
        
        rootval = postorder[-1]
        root = TreeNode(rootval)

        separate_idx = inorder.index(rootval)

        inorder_left = inorder[:separate_idx]
        inorder_right = inorder[separate_idx+1:]

        postorder.pop()
        postorder_left = postorder[:len(inorder_left)]
        postorder_right = postorder[len(inorder_left):]

        root.left = self.buildTree(inorder_left, postorder_left)
        root.right = self.buildTree(inorder_right, postorder_right)

        return root

从前序与中序遍历序列构造二叉树 (LC 105)

代码实现:

class Solution(object):
    def buildTree(self, preorder, inorder):
        """
        :type preorder: List[int]
        :type inorder: List[int]
        :rtype: TreeNode
        """

        if len(preorder) == 0:
            return None
        
        rootval = preorder[0]
        root = TreeNode(rootval)

        separate_index = inorder.index(rootval)

        inorder_left = inorder[:separate_index]
        inorder_right = inorder[separate_index+1:]

        preorder_left = preorder[1:len(inorder_left)+1]
        preorder_right = preorder[len(inorder_left)+1:]

        root.left = self.buildTree(preorder_left, inorder_left)
        root.right = self.buildTree(preorder_right, inorder_right) 
               
        return root
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值