1367. Linked List in Binary Tree ## 思路 掃整個tree 用recursion檢查tree_node跟lst_node 如果lst_node跑完就回傳True ## Code ```python class Solution: def isSubPath(self, head: Optional[ListNode], root: Optional[TreeNode]) -> bool: def check(tree_node, lst_node): if not lst_node: return True if not tree_node: return False if tree_node.val != lst_node.val: return False return check(tree_node.left, lst_node.next) or check(tree_node.right, lst_node.next) queue = deque([root]) while queue: node = queue.popleft() if check(node, head): return True if node.left: queue.append(node.left) if node.right: queue.append(node.right) return False ``` -- https://i.imgur.com/kyBhy6o.jpeg
-- ※ 發信站: 批踢踢實業坊(web-ptt.org.tw), 來自: 185.213.82.78 (臺灣) ※ 文章網址: https://web-ptt.org.tw/Marginalman/M.1725689787.A.598
DJYOMIYAHINA: 別捲了 09/07 14:17
sustainer123: 大師 09/07 14:20