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

看板Marginalman作者 (死肥肥社管)時間1年前 (2024/04/16 09:58), 1年前編輯推噓2(200)
留言2則, 2人參與, 1年前最新討論串123/1548 (看更多)
: https://leetcode.com/problems/add-one-row-to-tree : 623. Add One Row to Tree : 給你一個二元樹,請在深度為depth的位置插入一列值為val的節點。 今天遞迴解起來還蠻順的 感覺有點手感了 class Solution(object): def addOneRow(self, root, val, depth): """ :type root: TreeNode :type val: int :type depth: int :rtype: TreeNode """ if depth == 1: return TreeNode(val, left=root) if depth == 2: if root: tmpLeft = TreeNode(val, left=root.left) tmpRight = TreeNode(val, right=root.right) root.left = tmpLeft root.right = tmpRight elif depth > 2: if root.left: self.addOneRow(root.left, val, depth-1) if root.right: self.addOneRow(root.right, val, depth-1) return root -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.228.105.80 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1713232714.A.730.html ※ 編輯: argorok (36.228.105.80 臺灣), 04/16/2024 10:01:17

04/16 10:01, 1年前 , 1F
別卷了
04/16 10:01, 1F

04/16 10:08, 1年前 , 2F
別捲了
04/16 10:08, 2F
文章代碼(AID): #1c7TjASm (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1c7TjASm (Marginalman)