From 281a88102325a2cfe7bcf06c4788cfee0362d399 Mon Sep 17 00:00:00 2001 From: firepunch Date: Sun, 17 Mar 2024 01:04:36 -0700 Subject: [PATCH] Add week7 problems --- mirim/2024-03-17-buildTree-105.py | 17 ++++++++++++ mirim/2024-03-17-buildTree-106.py | 18 +++++++++++++ mirim/2024-03-17-distanceK.py | 44 +++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 mirim/2024-03-17-buildTree-105.py create mode 100644 mirim/2024-03-17-buildTree-106.py create mode 100644 mirim/2024-03-17-distanceK.py diff --git a/mirim/2024-03-17-buildTree-105.py b/mirim/2024-03-17-buildTree-105.py new file mode 100644 index 00000000..6d13726d --- /dev/null +++ b/mirim/2024-03-17-buildTree-105.py @@ -0,0 +1,17 @@ +# https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/ +# 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 buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: + if inorder: + index = inorder.index(preorder.pop(0)) + root = TreeNode(inorder[index]) + root.left = self.buildTree(preorder, inorder[:index]) + root.right = self.buildTree(preorder, inorder[index + 1 :]) + return root diff --git a/mirim/2024-03-17-buildTree-106.py b/mirim/2024-03-17-buildTree-106.py new file mode 100644 index 00000000..51b3ce96 --- /dev/null +++ b/mirim/2024-03-17-buildTree-106.py @@ -0,0 +1,18 @@ +# https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ +# 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 buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: + if not inorder or not postorder: + return None + root = TreeNode(postorder[-1]) + index = inorder.index(postorder[-1]) + root.left = self.buildTree(inorder[:index], postorder[:index]) + root.right = self.buildTree(inorder[index + 1 :], postorder[index:-1]) + return root diff --git a/mirim/2024-03-17-distanceK.py b/mirim/2024-03-17-distanceK.py new file mode 100644 index 00000000..1dfc8c37 --- /dev/null +++ b/mirim/2024-03-17-distanceK.py @@ -0,0 +1,44 @@ +# https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/description/ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + + +class Solution: + def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]: + res = [] + if k == 0: + res = [target.val] + return res + + graph = collections.defaultdict(list) + q = collections.deque([root]) + while q: + node = q.popleft() + if node.left: + graph[node].append(node.left) + graph[node.left].append(node) + q.append(node.left) + if node.right: + graph[node].append(node.right) + graph[node.right].append(node) + q.append(node.right) + + visited = set([target]) + q = collections.deque([target]) + level = 0 + while q: + for i in range(len(q)): + node = q.popleft() + if level == k: + res.append(node.val) + else: + for edge in graph[node]: + if edge not in visited: + visited.add(edge) + q.append(edge) + level += 1 + return res