Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/09/07 14:16), 編輯推噓1(101)
留言2則, 2人參與, 1年前最新討論串821/1548 (看更多)
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
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.78 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1725689787.A.598.html

09/07 14:17, 1年前 , 1F
別捲了
09/07 14:17, 1F

09/07 14:20, 1年前 , 2F
大師
09/07 14:20, 2F
文章代碼(AID): #1cs--xMO (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cs--xMO (Marginalman)