Programmie 2019-10-24 01:12 采纳率: 0%
浏览 229
已采纳

学习数据结构二叉树的一个问题

class BinTNode:
    def __init__(self, dat, left=None, right=None):
        self.data = dat
        #左右子树
        self.left = left
        self.right = right

def print_BinTNodes(t, tree=""):
        if t is None:
        #表示空树输出
        print("^", end="")
        return
    print("(" + str(t.data), end="")
    print_BinTNodes(t.left)
    print_BinTNodes(t.right)
    print(")", end="")
    #output:(1(2(5^^)^)(3^^))
    #need to be:(1(2^(5^^))(3^^))
    #why?

t = BinTNode(1, BinTNode(2,BinTNode(5)), BinTNode(3))
print_BinTNodes(t)

最后理应输出(1(2^(5^^))(3^^))
但是我的输出是(1(2(5^^)^)(3^^)),2节点的两个子树位置反了,请问是为什么呢

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-10-24 01:37
    关注

    你的代码不完整,你怎么构造的二叉树的代码在哪里,是不是写错了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?