我的代码应该在输入后确定并显示二叉树的数量。
我不断收到can't convert int object to str implicitly
错误,我不知道如何解决它。它很容易在 3.0 以下的 Python 版本中工作,所以请帮忙,因为我还是 Python 的初学者,我想了解我做错了什么。
import sys
print ("Welcome to Binary Tree Enumeration!")
x = input("Type an integer to output its binary trees: ")
print ("\nYou entered " + str(x))
def distinct(x):
leafnode = '.'
dp = []
newset = set()
newset.add(leafnode)
dp.append(newset)
for i in range(1,x):
newset = set()
for j in range(i):
for leftchild in dp[j]:
for rightchild in dp[i-j-1]:
newset.add(("(") + leftchild + rightchild + (")"))
dp.append(newset)
return dp[-1]
alltrees = distinct(x+1)
for tree in alltrees:
print (tree)
print ("Thank you for trying this out!")
我忘了补充......这是我得到的错误。
Traceback (most recent call last):
File "main.py", line 29, in
alltrees = distinct(x+1)
TypeError: Can't convert 'int' object to str implicitly