Tree Medium** **

题目

给定一个二叉树,返回它的 中序、前序 遍历。

  1. 输入: [1,null,2,3]
  2. 1
  3. \
  4. 2
  5. /
  6. 3
  7. 中序输出: [1,3,2]
  8. 前序输出: [1,2,3]
  9. 进阶: 递归算法很简单,你可以通过迭代算法完成吗?

思路

这里分别给出了三种二叉树的遍历方法与N叉树的前序遍历,及其时空复杂度

  1. 递归:
    1. 直接递归版本;
    2. 针对不同题目通用递归版本(包括前序、中序、后序)
  2. 迭代:
    1. 最常用版本(常用主要包括前序和层序,即【DFS和BFS】;
    2. 【前中后】序遍历通用版本(一个栈的空间);
    3. 【前中后层】序通用版本(双倍栈(队列)的空间)

      代码实现

      ```python

      Definition for a binary tree node.

      class TreeNode:

      def init(self, x):

      self.val = x

      self.left = None

      self.right = None

递归

时间复杂度:O(n),n为节点数,访问每个节点恰好一次。

空间复杂度:空间复杂度:O(h),h为树的高度。最坏情况下需要空间O(n),平均情况为O(logn)

递归1:二叉树遍历最易理解和实现版本

class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: if not root: return []

    # 前序递归 根 -> 左 -> 右
    return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right)
    # # 中序递归 左 -> 根 -> 右
    # return self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)
    # # 后序递归 左 -> 右 -> 根
    # return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val]
```python
# 递归2:通用模板,可以适应不同的题目,添加参数、增加返回条件、修改进入递归条件、自定义返回值
class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:
        def dfs(cur):
            if not cur:
                return      
            # 前序递归
            res.append(cur.val)  # 根
            dfs(cur.left)  # 左
            dfs(cur.right)  # 右

            # # 中序递归
            # dfs(cur.left)
            # res.append(cur.val)
            # dfs(cur.right)

            # # 后序递归
            # dfs(cur.left)
            # dfs(cur.right)
            # res.append(cur.val)      
        res = []
        dfs(root)
        return res
# 迭代
# 时间复杂度:O(n),n为节点数,访问每个节点恰好一次。
# 空间复杂度:O(h),h为树的高度。取决于树的结构,最坏情况存储整棵树,即O(n)

# 迭代1:前序遍历最常用模板(后序同样可以用)
class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:
        if not root:
            return []        
        res = []
        stack = [root]
        # # 前序迭代模板:最常用的二叉树DFS迭代遍历模板
        while stack:
            cur = stack.pop()
            res.append(cur.val)
            if cur.right:
                stack.append(cur.right)
            if cur.left:
                stack.append(cur.left)
        return res

        # # 后序迭代,相同模板:将前序迭代进栈顺序稍作修改,最后得到的结果反转
        # while stack:
        #     cur = stack.pop()
        #     if cur.left:
        #         stack.append(cur.left)
        #     if cur.right:
        #         stack.append(cur.right)
        #     res.append(cur.val)
        # return res[::-1]
# 迭代1:层序遍历最常用模板
class Solution:
    def levelOrder(self, root: TreeNode) -> List[List[int]]:
        if not root:
            return []
        cur, res = [root], []
        while cur:
            lay, layval = [], []
            for node in cur:
                layval.append(node.val)
                if node.left: lay.append(node.left)
                if node.right: lay.append(node.right)
            cur = lay
            res.append(layval)
        return res



# 迭代2:前、中、后序遍历通用模板(只需一个栈的空间)
class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]: 
        res = []
        stack = []
        cur = root
        # 中序,模板:先用指针找到每颗子树的最左下角,然后进行进出栈操作
        while stack or cur:
            while cur:
                stack.append(cur)
                cur = cur.left
            cur = stack.pop()
            res.append(cur.val)
            cur = cur.right
        return res

        # # 前序,相同模板
        # while stack or cur:
        #     while cur:
        #         res.append(cur.val)
        #         stack.append(cur)
        #         cur = cur.left
        #     cur = stack.pop()
        #     cur = cur.right
        # return res

        # # 后序,相同模板
        # while stack or cur:
        #     while cur:
        #         res.append(cur.val)
        #         stack.append(cur)
        #         cur = cur.right
        #     cur = stack.pop()
        #     cur = cur.left
        # return res[::-1]
# 迭代3:标记法迭代(需要双倍的空间来存储访问状态):
# 前、中、后、层序通用模板,只需改变进栈顺序或即可实现前后中序遍历,
# 而层序遍历则使用队列先进先出。0表示当前未访问,1表示已访问。
class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:
        res = []
        stack = [(0, root)]
        while stack:
            flag, cur = stack.pop()
            if not cur: continue
            if flag == 0:
                # 前序,标记法
                stack.append((0, cur.right))
                stack.append((0, cur.left))
                stack.append((1, cur))

                # # 后序,标记法
                # stack.append((1, cur))
                # stack.append((0, cur.right))
                # stack.append((0, cur.left))

                # # 中序,标记法
                # stack.append((0, cur.right))
                # stack.append((1, cur))
                # stack.append((0, cur.left))  
            else:
                res.append(cur.val)  
        return res

        # # 层序,标记法
        # res = []
        # queue = [(0, root)]
        # while queue:
        #     flag, cur = queue.pop(0)  # 注意是队列,先进先出
        #     if not cur: continue
        #     if flag == 0:
                  # 层序遍历这三个的顺序无所谓,因为是队列,只弹出队首元素
        #         queue.append((1, cur))
        #         queue.append((0, cur.left))
        #         queue.append((0, cur.right))
        #     else:
        #         res.append(cur.val)
        # return res

作者:821218213 链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/solution/python3-er-cha-shu-suo-you-bian-li-mo-ban-ji-zhi-s/