精華區beta Marginalman 關於我們 聯絡資訊
原本還想說是不是要inplace 發現我想不到 我好爛 還是用個平庸的方法比較好 def balanceBST(self, root: TreeNode) -> TreeNode: nodes = [] def dfs(root): nonlocal nodes if root is None: return dfs(root.left) nodes.append(root.val) dfs(root.right) dfs(root) def build(l,r) -> TreeNode: if l==r: return TreeNode(nodes[l]) elif r<l: return None mid = (l+r)//2 n = TreeNode(nodes[mid]) n.left = build(l,mid-1) n.right = build(mid+1,r) return n return build(0, len(nodes)-1) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.76.3.7 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1719413579.A.F0D.html
sustainer123: 大師 06/26 23:06
rainkaras: 大師 06/26 23:23