当前位置:   article > 正文

力扣(2024.08.07)

力扣(2024.08.07)

1. 637:二叉树的层平均值

  1. # Definition for a binary tree node.
  2. # class TreeNode:
  3. # def __init__(self, val=0, left=None, right=None):
  4. # self.val = val
  5. # self.left = left
  6. # self.right = right
  7. class Solution:
  8. def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
  9. def dfs(node, level, res):
  10. if not node:
  11. return
  12. if len(res) == level:
  13. res.append([])
  14. res[level].append(node.val)
  15. dfs(node.left, level + 1, res)
  16. dfs(node.right, level + 1, res)
  17. res = []
  18. level = 0
  19. dfs(root, level, res)
  20. final_res = []
  21. for i in res:
  22. final_res.append(sum(i) / len(i))
  23. return final_res

 2. 429:N叉树的层序遍历

  1. """
  2. # Definition for a Node.
  3. class Node:
  4. def __init__(self, val=None, children=None):
  5. self.val = val
  6. self.children = children
  7. """
  8. class Solution:
  9. def levelOrder(self, root: 'Node') -> List[List[int]]:
  10. def dfs(node, level, res):
  11. if not node:
  12. return
  13. if len(res) == level:
  14. res.append([])
  15. res[level].append(node.val)
  16. for child in node.children:
  17. dfs(child, level + 1, res)
  18. res = []
  19. level = 0
  20. dfs(root, level, res)
  21. return res

 3. 515:在每个树行中找最大值

  1. # Definition for a binary tree node.
  2. # class TreeNode:
  3. # def __init__(self, val=0, left=None, right=None):
  4. # self.val = val
  5. # self.left = left
  6. # self.right = right
  7. class Solution:
  8. def largestValues(self, root: Optional[TreeNode]) -> List[int]:
  9. def dfs(node, level, res):
  10. if not node:
  11. return
  12. if len(res) == level:
  13. res.append([])
  14. res[level].append(node.val)
  15. dfs(node.left, level + 1, res)
  16. dfs(node.right, level + 1, res)
  17. res = []
  18. level = 0
  19. dfs(root, level, res)
  20. final_res = []
  21. for i in res:
  22. final_res.append(max(i))
  23. return final_res

  4. 104:二叉树的最大深度

  1. # Definition for a binary tree node.
  2. # class TreeNode:
  3. # def __init__(self, val=0, left=None, right=None):
  4. # self.val = val
  5. # self.left = left
  6. # self.right = right
  7. class Solution:
  8. def maxDepth(self, root: Optional[TreeNode]) -> int:
  9. def dfs(node, level, res):
  10. if not node:
  11. return
  12. if len(res) == level:
  13. res.append([])
  14. res[level].append(node.val)
  15. dfs(node.left, level + 1, res)
  16. dfs(node.right, level + 1, res)
  17. res = []
  18. level = 0
  19. dfs(root, level, res)
  20. return len(res)

   5. 226:翻转二叉树

  1. # Definition for a binary tree node.
  2. # class TreeNode:
  3. # def __init__(self, val=0, left=None, right=None):
  4. # self.val = val
  5. # self.left = left
  6. # self.right = right
  7. class Solution:
  8. def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
  9. def dfs(node):
  10. if not node:
  11. return
  12. node.left, node.right = node.right, node.left
  13. dfs(node.left)
  14. dfs(node.right)
  15. dfs(root)
  16. return root

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号