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

看板Marginalman作者 (みけねこ的鼻屎)時間2年前 (2023/02/16 09:20), 編輯推噓4(400)
留言4則, 4人參與, 2年前最新討論串235/719 (看更多)
104. Maximum Depth of Binary Tree 給你一個二元樹,求出他的最大深度。 Example: https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg
Input: root = [3,9,20,null,null,15,7] Output: 3 思路: 1.dfs這個樹如果當前節點不為空則深度+1,並且加上左右子樹裡面比較大的深度就 是最大深度 JavaCode: ---------------------------------------- class Solution { public int maxDepth(TreeNode root) { if (root == null) { return 0; } return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } } ---------------------------------------- -- https://i.imgur.com/3e5CZfj.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.160.89.182 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1676510444.A.03F.html

02/16 09:21, 2年前 , 1F
大師
02/16 09:21, 1F

02/16 09:24, 2年前 , 2F
大師
02/16 09:24, 2F

02/16 11:22, 2年前 , 3F
大師
02/16 11:22, 3F

02/16 13:54, 2年前 , 4F
大師
02/16 13:54, 4F
文章代碼(AID): #1ZxOJi0_ (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1ZxOJi0_ (Marginalman)