Day17: 二叉树之路, 道阻且长啊
创始人
2024-03-25 12:52:52
0

代码随想录算法训练营Day17

110. Balanced Binary Tree

!!! 平衡二叉树的左右子树仍为平衡二叉树.

A height balanced binary tree is a binary tree in which the height of the left subtree and right subtree of any node does not differ by more than 1 and both the left and right subtree are also height balanced.

关于根节点的深度究竟是1 还是 0,不同的地方有不一样的标准,leetcode的题目中都是以节点为一度,即根节点深度是1。但维基百科上定义用边为一度,即根节点的深度是0,我们暂时以leetcode为准(毕竟要在这上面刷题)。

遇到的问题:因为在递归过程中,需要比较深度,同时还要传出True or False. 不知如何让其协调.

class Solution:def isBalanced(self, root: Optional[TreeNode]) -> bool:def depthBalanced(root: TreeNode) -> int:if not root: return 0dLeft = depthBalanced(root.left)if dLeft == -1: return -1 # 错在这一步dRight = depthBalanced(root.right)if dRight == -1: return -1if abs(dLeft - dRight)> 1:return -1else:return max(dLeft, dRight)+1if not root: return Truereturn False if depthBalanced(root) == -1 else True# TC: O(N)# SC: O(N)

257. Binary Tree Paths

终止条件 不需要判断 root == None

class Solution:def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:# 后序递归# 条件指出root非空res = []def path(root: TreeNode, s: str):if not(root.left or root.right): res.append(s)if root.right:             path(root.right,s + "->"+str(root.right.val))if root.left:           path(root.left, s + "->"+str(root.left.val))returnpath(root, str(root.val))return res# TC: O(N)# SC: O(N)

前序迭代

class Solution:def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:# 前序迭代res = []stack = []cur = rootpath = str(root.val)stack.append((cur, path))while stack:cur, path = stack.pop()if not(cur.left or cur.right):res.append(path)if cur.right: stack.append((cur.right, path+"->"+str(cur.right.val)))if cur.left: stack.append((cur.left, path+"->"+str(cur.left.val)))return res

404. Sum of Left Leaves

Sol1 层序迭代

class Solution:def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:# 层序迭代if not root: return 0q = [root]res = [] # save the value of all leaveswhile q:for _ in range(len(q)):cur = q.pop(0)if cur.left: q.append(cur.left)if not(cur.left.left or cur.left.right): res.append(cur.left.val)if cur.right: q.append(cur.right)return sum(res)

Sol2 前序递归

        # Sol2 前序递归if not root: return 0if not(root.left or root.right): return 0sum = 0if root.left and not(root.left.left) and not(root.left.right):sum += root.left.valsum += self.sumOfLeftLeaves(root.left)sum += self.sumOfLeftLeaves(root.right)return sum

相关内容

热门资讯

每周股票复盘:瀚川智能(688... 截至2025年12月26日收盘,瀚川智能(688022)报收于15.3元,较上周的14.42元上涨6...
每周股票复盘:中粮糖业(600... 截至2025年12月26日收盘,中粮糖业(600737)报收于17.27元,较上周的17.18元上涨...
每周股票复盘:马钢股份(600... 截至2025年12月26日收盘,马钢股份(600808)报收于4.22元,较上周的3.82元上涨10...
每周股票复盘:内蒙一机(600... 截至2025年12月26日收盘,内蒙一机(600967)报收于16.34元,较上周的16.05元上涨...
富达基金投顾业务负责人戴旻:封... 由三亚市人民政府主办,《财经》杂志、财经网、《财经智库》、三亚中央商务区管理局、三亚经济研究院承办的...
海南大谷国际园区董事长张焱:以... 由三亚市人民政府主办,《财经》杂志、财经网、《财经智库》、三亚中央商务区管理局、三亚经济研究院承办的...
每周股票复盘:汉马科技(600... 截至2025年12月26日收盘,汉马科技(600375)报收于5.87元,较上周的5.92元下跌0....
每周股票复盘:中天科技(600... 截至2025年12月26日收盘,中天科技(600522)报收于18.4元,较上周的18.37元上涨0...
网约配送员权益维护有了专门法规... 今年厦门市出台《厦门经济特区网约配送员劳动权益保障若干规定》(以下简称《若干规定》),这是厦门市在全...
“十四五”的大冶答卷:政策赋能... 从商业综合体崛起到乡村直播间走红,从政策红利直达市场主体到“大冶制造”扬帆出海……“十四五”期间,大...