[WUSTCTF 2020]level4

文章介绍了如何在无壳64位的ELF程序中,利用inorder和postorder遍历顺序构建二叉树,解决特定问题。提到`find`方法的应用和在有重复字符的情况下可能存在的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

无壳64位的ELF程序
放入IDA
在这里插入图片描述进入函数发现
type1 中序遍历
type2 后序遍历
type3 前序遍历
在linux下运行
在这里插入图片描述那这应该前序遍历就是flag

def preOrderByInPost(inorder,postorder):
    if len(inorder)==0 or len(postorder)==0:
        return
    root_val=postorder[-1]# 根节点为后序遍历的最后一个元素
    print(postorder[-1],end="")# 也就是前序遍历的第一个元素
    root_index = inorder.find(root_val)
    preOrderByInPost(inorder[:root_index],postorder[:root_index])
    preOrderByInPost(inorder[root_index+1:],postorder[root_index:len(postorder)-1])
inorder ="2f0t02T{hcsiI_SwA__r7Ee}"
postorder="20f0Th{2tsIS_icArE}e7__w"
preOrderByInPost(inorder,postorder)

得到flag
在这之前还写了已知前序遍历和中序遍历求完整二叉树的代码
但是这题用不了,因为有重复字符,inorder.find()找根节点不准确

class TreeNode:
    def __init__(self,val,left=None,right=None):
        self.val=val
        self.left=left
        self.right=right
#已知前序遍历和中序遍历
def buildTreeForPost(preorder,inorder):
    if not preorder or not inorder:
        return None
    root_val=preorder[0]        #根节点为前序遍历的第一个元素
    root=TreeNode(root_val)     #构造根节点
    root_index=inorder.find(root_val)#在中序遍历中找到这个根节点  列表的话使用inorder.index
    #递归构建左子树和右子树
    root.left=buildTreeForPost(preorder[1:1+root_index],inorder[:root_index])
    root.right=buildTreeForPost(preorder[1+root_index:],inorder[root_index+1:])
    return root
#已知后序遍历和中序遍历
def buildTreeForPre(inorder,postorder):
    if not postorder or not inorder:
        return None
    root_val=postorder[len(postorder)-1]        #根节点为后序遍历的最后一个元素
    root=TreeNode(root_val)     #构造根节点
    root_index=inorder.find(root_val)#在中序遍历中找到这个根节点  列表的话使用inorder.index
    #递归构建左子树和右子树
    root.left=buildTreeForPre(inorder[:root_index],postorder[:root_index])
    root.right=buildTreeForPre(inorder[root_index+1:],postorder[root_index:len(postorder)-1])
    return root
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值