二叉树的前序遍历,中序遍历和后序遍历(python实现)

 

前序遍历

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

class Solution(object):
    def preorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root == None:
            return []
        resLs = []
        stack,node = [],root
        while stack.__len__() > 0 or node!=None:
            while node != None:
                resLs.append(node.val)
                stack.append(node)
                node = node.left
            node = stack.pop()
            node = node.right
        return resLs

        

中序遍历

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

class Solution(object):
    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root == None:
            return []
        stack,node = [],root
        resLs = []
        while stack.__len__() > 0 or node!=None:  
            while node!=None:  ###向左叶子节点遍历直到最底层
                stack.append(node)
                node = node.left
            node = stack.pop()
            resLs.append(node.val)
            node = node.right
        return resLs

后序遍历(基本思想:通过层次遍历,总是先打印右节点,然后再使用一次倒序来打印所有结果;例如存在树结构1->2,1->3,2->4,2->5,3->6,3->7;可以按照顺序(1,3,7,6,2,5,4)的顺序将结果存储起来,然后再进行一次逆序就得到正确结果)

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

class Solution(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root == None:
            return []
        stack,node = [root],root
        resLs = []
        while stack.__len__()>0:  
            node = stack.pop()
            resLs.append(node.val)
            if node.left != None:
                stack.append(node.left)
            if node.right != None:
                stack.append(node.right)
        return [val for val in reversed(resLs)]
        
        

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。(基本思想:二叉搜索树满足左子节点值都小于根节点值,右子节点值都大于根节点值;后序遍历的最后一个元素是根节点,序列中小于根节点的所有数构成左子树,大于根节点的所有数构成右子树)

# -*- coding:utf-8 -*-
class Solution:
    def VerifySquenceOfBST(self, sequence):
        # write code here
        if len(sequence)==0:
            return False
        index = 0
        for index in xrange(len(sequence)-1):  ###找出左子树
            if sequence[index]>sequence[-1]:
                break
        for i in xrange(index+1,len(sequence)-1):  ###判断右子树的数是否比根节点大
            if sequence[i]<sequence[-1]:
                return False
        if self.check(sequence,0,index) and self.check(sequence,index+1,len(sequence)-1):
            return True
        else:
            return False
    def check(self,sequence,start,end):
        if start>=end:
            return True
        index = 0
        for index in xrange(start,end):
            if sequence[index]>sequence[end]:
                break
        for i in xrange(index+1,end):
            if sequence[i]<sequence[end]:
                return False
        return self.check(sequence,start,index) and self.check(sequence,index+1,end-1)
        



 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值