Re: [閒聊] 每日LeetCode已回收

看板Marginalman作者 (みけねこ的鼻屎)時間3年前 (2022/10/04 09:52), 編輯推噓3(300)
留言3則, 3人參與, 3年前最新討論串27/719 (看更多)
112. Path Sum 說明: 給予一個二元樹和一個數字target,從root到葉節點的路徑稱為path,若存在任意path中 所有數字和等於target返回true,否則返回false。 Example 1: https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: The root-to-leaf path with the target sum is shown. 解法: 1.DFS中序遍歷並更新targetSum的值 2.若root是葉子節點且等於targetSum返回true Java Code: class Solution { public boolean hasPathSum(TreeNode root, int targetSum) { if(root == null) return false; if(targetSum == root.val && root.left == null && root.right == null) return true; return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); } } 樹 = 溫柔善良 -- https://i.imgur.com/OTNN84M.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.160.85.207 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1664848365.A.CC7.html

10/04 09:58, 3年前 , 1F
好像大學計程上機考會出現的題目
10/04 09:58, 1F

10/04 10:17, 3年前 , 2F
大師
10/04 10:17, 2F

10/04 10:28, 3年前 , 3F
大師
10/04 10:28, 3F
文章代碼(AID): #1ZEv7jp7 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1ZEv7jp7 (Marginalman)