zoukankan      html  css  js  c++  java
  • 二叉树遍历

    二叉树递归遍历

    # 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 inorderTraversal(self, root: TreeNode) -> List[int]:
            # 前序
            perv = []
            # 中序
            mid = []
            # 后序
            last = []
            def find(root):
               if not root:
                   return
               perv.append(root.val)
               find(root.left)
               mid.append(root.val)
               find(root.right)
               last.append(root.val)
            find(root)
            return [prev, mid, last]
    

    二叉树非递归前序遍历(中左右)

    # 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 postorderTraversal(self, root: TreeNode) -> List[int]:
            if not root:
                return []
            res = []
            stack = [root]
            while stack:
                node = stack.pop()
                res.append(node.val)
                if node.right:
                    stack.append(node.right)
                if node.left:
                    stack.append(node.left)
            return res
    

    二叉树非递归中序遍历(左中右)

    # 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 inorderTraversal(self, root: TreeNode) -> List[int]:
            stack = []
            res = []
            while root or stack:
                if root:
                    stack.append(root)
                    root = root.left
                else:
                    node = stack.pop()
                    res.append(node.val)
                    root = node.right
            return res
    

    二叉树非递归后序遍历(左右中 ==> 前序(中左右)> 中右左(反转))

    # 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 postorderTraversal(self, root: TreeNode) -> List[int]:
            """
            后序等于前序(左右)的变换, 然后反转 得到
            """
            if not root:
                return []
            res = []
            stack = [root]
            while stack:
                node = stack.pop()
                res.append(node.val)
                if node.left:
                    stack.append(node.left)
                if node.right:
                    stack.append(node.right)
            # 翻转
            return res[::-1]
    
    此时此刻,非我莫属
  • 相关阅读:
    被遗忘的Ruby Web开发框架
    批处理设置IP地址
    Java集合类ArrayList,Vector,HashMap,Hashtable区别
    eclipse安装Eclipse HTML Editor插件
    安装MYSQL向导时,到最后一步 Mysql server instance configuration wizard 单击完成时没反响应?
    各种缓存综述
    linux下apache字符集问题
    ubuntu下图形界面软件问题综述
    linux1xh3c802.11在ubuntu下联网
    REMOTE_ADDR,HTTP_CLIENT_IP,HTTP_X_FORWARDED_FOR
  • 原文地址:https://www.cnblogs.com/taozhengquan/p/15354375.html
Copyright © 2011-2022 走看看