代码随想录算法训练营DAY18|513.找树左下角的值、 112. 路径总和、113.路径总和i、106.从中序与后序遍历序列构造二叉树、105.从前序与中序遍历序列构造二叉树

补一下。

513.找树左下角的值

# 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 findBottomLeftValue(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return []

        queue = collections.deque([root])
        result = []

        while queue:
            level = []
            for _ in range(len(queue)):
                node = queue.popleft()
                level.append(node.val)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            if not queue:
                return level[0]

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
        """
        if not root:
            return False
        st = collections.deque([root])
        sum_total = [root.val]

        while st:
            cur = st.pop()
            sum_tmp = sum_total.pop()
            if not cur.left and not cur.right:
                if sum_tmp == targetSum:
                    return True
            if cur.right:
                st.append(cur.right)
                sum_total.append(sum_tmp+cur.right.val)
            if cur.left:
                st.append(cur.left)
                sum_total.append(sum_tmp+cur.left.val)
            
        return False
  • 回溯
# 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
        """
        if not root:
            return False

        if not root.left and not root.right:
            if targetSum==root.val:
                return True
        return self.hasPathSum(root.right, targetSum-root.val) or self.hasPathSum(root.left, targetSum-root.val)

113.路径总和ii

  • 题目链接:113.路径总和ii
  • 回溯
    • 注意result和path的位置,以及result.append(path[:])
# 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 __init__(self):
        self.result = []
        self.path = []
    def traversal(self, root, targetSum):
        print(targetSum)
        self.path.append(root.val)
        if not root.left and not root.right:
            if sum(self.path)==targetSum:
                self.result.append(self.path[:])
        if root.left:
            self.traversal(root.left, targetSum)
            self.path.pop()
        if root.right:
            self.traversal(root.right, targetSum)
            self.path.pop()
        return

    def pathSum(self, root, targetSum):
        """
        :type root: TreeNode
        :type targetSum: int
        :rtype: List[List[int]]
        """
        if not root:
            return self.result

        self.traversal(root, targetSum)

        return self.result

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

# 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 buildTree(self, inorder, postorder):
        """
        :type inorder: List[int]
        :type postorder: List[int]
        :rtype: TreeNode
        """
        if inorder==[-1]:
            return TreeNode(val=-1)
        if not inorder:
            return None
        
        root=TreeNode(val=postorder[-1])
        root.left=self.buildTree(inorder[:inorder.index(root.val)],postorder[:inorder.index(root.val)])
        
        root.right=self.buildTree(inorder[inorder.index(root.val)+1:],postorder[inorder.index(root.val):-1])
                             
        return root
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值