欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-03-19 22:53:28
...

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

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

 输出: [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]


广度优先

使用队列:前出中压左右