树的遍历:前、中、后序、广度优先

本文介绍树的遍历方式:前序遍历、中序遍历、后序遍历已经广度优先遍历

0.举例

比如上图二叉树遍历结果

    前序遍历:ABCDEFGHK

    中序遍历:BDCAEHGKF

    后序遍历:DCBHKGFEA

1.先序遍历:

 

leetcode144:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def preorderTraversal(self, root: TreeNode) -> List[int]:
        node = root
        if node is None:
            return []
        stack = [node]
        res = []
        while stack:
            pop=stack.pop()
            res.append(pop.val)
            if pop.right is not None:
                stack.append(pop.right)
            if pop.left is not None:
                stack.append(pop.left)
        return res
        # 递归方式
        # res = []
        # def qian(node,res):
        #     if node is None:
        #         return
        #     res.append(node.val)
        #     qian(node.left,res)
        #     qian(node.right,res)
        # qian(node,res)
        # return res

2.中序遍历 

leetcode94:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        stack = [root]
        res = []
        cur = root
        if root is None:
            return []
        while stack:
            while cur.left :
                cur = cur.left
                stack.append(cur)
            pop = stack.pop()
            res.append(pop.val)
            if pop.right:
                cur = pop.right
                stack.append(cur)
        return res
        # 递归
        # def z(cur):
        #     if cur:
        #         z(cur.left)
        #         res.append(cur.val)
        #         z(cur.right)
        # z(cur)
        # return res
        

3.后序遍历 

 

leetcode145: 

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        if root is None:
            return []
        cur = root
        res = []
        stack_i=[cur]
        stack_o=[]
        while stack_i:
            pop=stack_i.pop()
            stack_o.append(pop)
            if pop.left is not None:
                stack_i.append(pop.left)
            if pop.right is not None:
                stack_i.append(pop.right)
        while stack_o:
            res.append(stack_o.pop().val)
        return res        
        # 递归
        # def h(node):
        #     if node:
        #         h(node.left)
        #         h(node.right)
        #         res.append(node.val)
        # h(cur)
        # return res
            
                

 4.广度优先遍历:请参考上一篇博文。https://blog.csdn.net/qq_38742161/article/details/88958342

5. 深度优先遍历:即为前序遍历。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值