Day18 | 513. 找樹左下角的值 | 112.路徑總和、113.路徑總和ii

forrestr發表於2024-06-13

513. 找樹左下角的值

本題遞迴偏難,反而迭代簡單屬於模板題, 兩種方法掌握一下

題目連結/文章講解/影片講解:https://programmercarl.com/0513.找樹左下角的值.html

思考

層序遍歷秒了

# 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:
        queue = deque()
        queue.append(root)
        res = []
        while queue:
            level = []
            for i 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)
            res.append(level)
        return res[-1][0]

112.路徑總和、113.路徑總和ii

本題 又一次涉及到回溯的過程,而且回溯的過程隱藏的還挺深,建議先看影片來理解

  1. 路徑總和,和 113. 路徑總和ii 一起做了。 優先掌握遞迴法。

題目連結/文章講解/影片講解:https://programmercarl.com/0112.路徑總和.html

112.路徑總和
隱藏的回溯方法,sum每次提前減掉後傳到下一步的。

# 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 hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
        if root is None:
            return False
        if root.left is None and root.right is None:
            if root.val == targetSum:
                return True
            else:
                return False            
        elif root.left and not root.right:
            return self.hasPathSum(root.left,targetSum-root.val)
        elif not root.left and root.right:
            return self.hasPathSum(root.right,targetSum-root.val)
        else:
            return self.hasPathSum(root.right,targetSum-root.val) or self.hasPathSum(root.left,targetSum-root.val) 

113.路徑總和ii

# 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 __init__(self):
        self.sum = 0
    def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
        path = []
        res = []
        def backtracking(root,targetSum):
            if root is None:
                return
            path.append(root.val)
            if root.left is None and root.right is None:
                if self.sum + root.val == targetSum:
                    res.append(path[:])
                return 
            if root.left:
                self.sum+=root.val
                backtracking(root.left,targetSum)
                self.sum-=root.val
                path.pop()
            if root.right:
                self.sum+=root.val
                backtracking(root.right,targetSum)
                self.sum-=root.val
                path.pop()
        backtracking(root,targetSum)
        return res

從中序與後序遍歷序列構造二叉樹

本題算是比較難的二叉樹題目了,大家先看影片來理解。

106.從中序與後序遍歷序列構造二叉樹,105.從前序與中序遍歷序列構造二叉樹 一起做,思路一樣的

題目連結/文章講解/影片講解:https://programmercarl.com/0106.從中序與後序遍歷序列構造二叉樹.html

相關文章