二叉树遍历与递归及非递归实现方法

二叉树的遍历真的是编了忘忘了编,直接Mark到这边以便查阅。

树的相关知识:👉

堆的相关知识:👉数据结构:堆(Heap)

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

a = TreeNode(10)
a.left = TreeNode(6)
a.right = TreeNode(13)
a.left.left = TreeNode(4)
a.left.right = TreeNode(8)
a.right.left = TreeNode(0)
a.right.right = TreeNode(16)
a.right.left.left = TreeNode(1)

深度优先

前序遍历:先根再左后右DLR

def DSFbyRecurrentDLR(ListPrint,root): #(root)
    if root is not None:
        #print(root.val)
        ListPrint.append(root.val)
        if root.left is not None:
            DSFbyRecurrentDLR(ListPrint, root.left)
            pass
        if root.right is not None:
            DSFbyRecurrentDLR(ListPrint, root.right)
            pass

def testDSFbyRecurrentDLR(root):
    k = []
    DSFbyRecurrentDLR(k, root)
    print(k)

testDSFbyRecurrentDLR(a)
def DSFbyStackDLR(root):
    if root is not None:
        stack = []
        stack.append(root)
        while len(stack) :
            cur_node = stack.pop()
            print(cur_node.val)
            if cur_node.right is not None:
                stack.append(cur_node.right)
            if cur_node.left is not None:
                stack.append(cur_node.left)

DSFbyStackDLR(a)

 输出:[10, 6, 4, 8, 13, 0, 1, 16] 

中序遍历:先左再根后右 LDR 

def DSFbyRecurrentLDR(ListPrint, root): #(root)
    if root is not None:
        if root.left is not None:
            DSFbyRecurrentLDR(ListPrint, root.left)
            pass
        #print(root.val)
        ListPrint.append(root.val)
        if root.right is not None:
            DSFbyRecurrentLDR(ListPrint, root.right)
            pass

def testDSFbyRecurrentLDR(root):
    k = []
    DSFbyRecurrentLDR(k, root)
    print(k)

testDSFbyRecurrentLDR(a)

Warning: 压中左,弹左,再压右

def DSFbyStackLDR(root):
    if root is not None:
        # if the memory path is needed, a stack must be needed
        stack = []
        cur = root  # the scanning cur_p for rootTree
        while len(stack) or cur is not None:
            while cur is not None:
                stack.append(cur)
                cur = cur.left

            cur = stack.pop()
            print(cur.val)
            cur = cur.right

 中序的非递归算法和可以当作回溯法在二叉树上搜索的应用。

即若节点不存在,则寻找上一级节点。解空间就是树的val组成的空间,因此没有限制条件。

 输出: [4, 6, 8, 10, 1, 0, 13, 16]

后续遍历:先左再右后根 LRD

def DSFbyRecurrentLRD(ListPrint, root): #(root)
    if root is not None:
        if root.left is not None:
            ListPrint.append(DSFbyRecurrentLRD(ListPrint, root.left))
            #DSFbyRecurrent(root.left)
            pass
        if root.right is not None:
            ListPrint.append(DSFbyRecurrentLRD(ListPrint, root.right))
            # DSFbyRecurrent(root.right)
        #print(root.val)
        return root.val

def testDSFbyRecurrentLRD(root):
    k = []
    k.append(DSFbyRecurrentLRD(k, root))
    print(k)

testDSFbyRecurrentLRD(a)

Warning: STACK正常弹中压左右,弹去STACKSAVE,最后STACKSAVE弹出 

def DSFbyStackLRD(root):
    if root is not None:
        stack = []
        stack.append(root)
        stackSave = []
        #多用一个stackSave来承载
        while len(stack):
            cur_node = stack.pop()
            stackSave.append(cur_node)
            if cur_node.left is not None:
                stack.append(cur_node.left)
            if cur_node.right is not None:
                stack.append(cur_node.right)

        # print(stack)

        while len(stackSave):
            k = stackSave.pop().val
            print(k)

# DSFbyStackDLR(a)
DSFbyStackLRD(a)

 输出:[4, 8, 6, 1, 0, 16, 13, 10]


广度优先

使用队列:前出中压左右

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值