110.平衡二叉树
自己写了一版,辅助函数同时算高度和是否是平衡的。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def check(root, h):
if not root:
return h, True
l, lb = check(root.left, h + 1)
r, rb = check(root.right, h + 1)
return max(l, r), lb and rb and abs(l - r) < 2
_, res = check(root, 0)
return res
题解思路:
辅助函数只算高度,用-1表示不平衡了。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def height(root):
if not root:
return 0
l = height(root.left)
if l == -1 : return -1
r = height(root.right)
if r == -1: return -1
return -1 if abs(l - r) > 1 else 1 + max(l, r)
if not root:
return True
return False if height(root) == -1 else True
257. 二叉树的所有路径
回溯,遇到叶子节点就收集结果。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
res = []
def dfs(root, s):
if not root:
return
if not root.left and not root.right:
res.append(s + str(root.val))
return
dfs(root.left, s + str(root.val) + '->')
dfs(root.right, s + str(root.val) + '->')
dfs(root, '')
return res
404.左叶子之和
闭包传递,稀里糊涂也写对了
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
count = 0
def dfs(root):
nonlocal count
if not root:
return
if not root.left and not root.right:
return
if root.left:
if not root.left.left and not root.left.right:
count += root.left.val
else:
dfs(root.left)
if root.right:
dfs(root.right)
dfs(root)
return count
看一下题解,向左边探查递归返回时查看是否是父节点的左叶子,如果是的话就找到一个左叶子,记录值。向右探查时也会一起赋值,思路相对巧妙。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
if not root.left and not root.right:
return 0
left = self.sumOfLeftLeaves(root.left)
if root.left and not root.left.left and not root.left.right:
left = root.left.val
right = self.sumOfLeftLeaves(root.right)
return left + right
222.完全二叉树的节点个数
如果是个普通的二叉树,直接可以求解:
class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
return 1 + self.countNodes(root.left) + self.countNodes(root.right)
但是题目里面说了是个完全二叉树,完全二叉树可以节省时间,因为完全二叉树一个节点的左子树和右子树至少有一个是完全二叉树。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
left = root.left
right = root.right
l, r = 0, 0
while left:
left = left.left
l += 1
while right:
right = right.right
r += 1
if l == r:
return (2 << l) - 1
return self.countNodes(root.left) + self.countNodes(root.right) + 1

被折叠的 条评论
为什么被折叠?



