• 算法day 14 第六章二叉树


    今日内容:

    • 理论基础
    • 递归遍历
    • 迭代遍历
    • 统一迭代

    递归遍历 (必须掌握)

    三个要点:

    • 参数以及返回值
    • 终止条件
    • 单层循环逻辑

    前序遍历:中左右

    中序遍历:左中右

    后序遍历:左右中

    1. class Solution(object):
    2. def preorderTraversal(self, root):
    3. """
    4. :type root: TreeNode
    5. :rtype: List[int]
    6. """
    7. result =[]
    8. def Traversal(root):
    9. if root == None:
    10. return
    11. result.append(root.val)
    12. Traversal(root.left)
    13. Traversal(root.right)
    14. Traversal(root)
    15. return result
    • 145.二叉树的后序遍历(opens new window)
      1. class Solution(object):
      2. def postorderTraversal(self, root):
      3. """
      4. :type root: TreeNode
      5. :rtype: List[int]
      6. """
      7. result = []
      8. def traversal(root):
      9. if root == None:
      10. return
      11. traversal(root.left) # 左
      12. traversal(root.right) # 右
      13. result.append(root.val) # 后序
      14. traversal(root)
      15. return result

    中右左-反转变成左右中

      1. var postorderTraversal = function(root) {
      2. // 左右中
      3. let result = []
      4. let stack = []
      5. stack.push(root)
      6. while(stack.length){
      7. let node = stack.pop()
      8. if(node){
      9. result.push(node.val)
      10. if(node.left){
      11. stack.push(node.left)
      12. }
      13. if(node.right){
      14. stack.push(node.right)
      15. }
      16. }
      17. }
      18. return result.reverse()
      19. };

    • 94.二叉树的中序遍历
    1. class Solution(object):
    2. def inorderTraversal(self, root):
    3. """
    4. :type root: TreeNode
    5. :rtype: List[int]
    6. """
    7. #中序遍历:左中右
    8. result = []
    9. def traversal(root):
    10. if root is None:
    11. return
    12. traversal(root.left)
    13. result.append(root.val)
    14. traversal(root.right)
    15. traversal(root)
    16. return result

    递归法

    1. var inorderTraversal = function(root) {
    2. let result =[]
    3. const traversal = function(node){
    4. // 左中右
    5. if(node === null){
    6. return
    7. }
    8. if(node.left){
    9. traversal(node.left)
    10. }
    11. result.push(node.val)
    12. if(node.right){
    13. traversal(node.right)
    14. }
    15. return result
    16. }
    17. if(root === null){
    18. return []
    19. }
    20. return traversal(root)
    21. };

    迭代法

    1. var inorderTraversal = function(root) {
    2. let result = []
    3. let stack = []
    4. let cur = root
    5. while(stack.length||cur){
    6. if(cur){
    7. stack.push(cur)
    8. cur = cur.left
    9. }else{
    10. cur = stack.pop()
    11. result.push(cur.val)
    12. cur = cur.right
    13. }
    14. }
    15. return result
    16. };

    题目链接/文章讲解/视频讲解:代码随想录

    迭代遍历/统一迭代遍历 (基础不好的录友,迭代法可以放过)

    前序改为后序只需要三行代码:

    中左右->中右左->左右中

    访问节点/处理节点

    1. #前序
    2. class Solution(object):
    3. def preorderTraversal(self, root):
    4. """
    5. :type root: TreeNode
    6. :rtype: List[int]
    7. """
    8. stack = []
    9. result = []
    10. if root is not None:
    11. stack.append(root)
    12. while stack:
    13. node = stack.pop()
    14. if node is not None:
    15. if node.right is not None:
    16. stack.append(node.right)
    17. if node.left is not None:
    18. stack.append(node.left)
    19. stack.append(node)
    20. stack.append(None)
    21. else:
    22. node = stack.pop()
    23. result.append(node.val)
    24. return result
    1. #后序
    2. class Solution(object):
    3. def postorderTraversal(self, root):
    4. """
    5. :type root: TreeNode
    6. :rtype: List[int]
    7. """
    8. result = []
    9. stack = []
    10. if root:
    11. stack.append(root)
    12. while stack:
    13. node = stack.pop()
    14. if node != None:
    15. if node.left:
    16. stack.append(node.left)
    17. if node.right:
    18. stack.append(node.right)
    19. stack.append(node)
    20. stack.append(None)
    21. else:
    22. node = stack.pop()
    23. result.append(node.val)
    24. return reversed(result)
    1. # Definition for a binary tree node.
    2. # class TreeNode(object):
    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(object):
    8. #中序遍历
    9. def inorderTraversal(self, root):
    10. """
    11. :type root: TreeNode
    12. :rtype: List[int]
    13. """
    14. #中序遍历:左中右
    15. result = []
    16. stack = []
    17. if root:
    18. stack.append(root)
    19. while stack:
    20. node = stack.pop()
    21. if node is not None:
    22. if node.right is not None:
    23. stack.append(node.right)
    24. stack.append(node)
    25. stack.append(None)
    26. if node.left is not None:
    27. stack.append(node.left)
    28. else:
    29. node = stack.pop()
    30. result.append(node.val)
    31. return result

    如何标记呢,就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。 这种方法也可以叫做标记法。

    题目链接/文章讲解/视频讲解:代码随想录代码随想录

  • 相关阅读:
    C#教学辅助系统网站as.net+sqlserver
    Linux学习记录——사 权限与工具
    深度学习实践3:多层感知机
    在 4GB 物理内存的机器上,申请 8G 内存会怎么样?
    【爬虫+情感判定+Top10高频词+词云图】“谷爱凌”热门弹幕python舆情分析
    通过 HelpLook ChatBot AI自动问答机器人降低客户服务成本
    Netty实战(三)
    【C++程序员必修第一课】C++基础课程-15:struct 数据结构
    返乡人员信息登记管理系统,助力精准管控
    C++ QT QLocalSocket/QLocalServer基操
  • 原文地址:https://blog.csdn.net/weixin_42173016/article/details/127754554