马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class BinaryTree:
def __init__(self, num):
self.val = num
self.left = None
self.right = None
if type(num) == list:
self.num = num[0]
for n in num[1:]:
self.insert(n)
else:
self.num = num
def insert(self, num):
bt = self
while True:
if num <= bt.num:
if bt.left == None:
bt.left = BinaryTree(num)
break
else:
bt = bt.left
else:
if bt.right == None:
bt.right = BinaryTree(num)
break
else:
bt = bt.right
class Solution:
def binaryTreePaths(self, root):
def construct_paths(root, path):
if root:
path += str(root.num)
if not root.left and not root.right:
paths.append(path)
else:
path += '->'
construct_paths(root.left, path)
construct_paths(root.right, path)
paths = []
construct_paths(root, '')
return paths
num = list(map(int,input().split()))
bt = BinaryTree(num)
s = Solution()
l = s.binaryTreePaths(bt)
for i in l:
print(i)
老师布置了一道题死活看不懂求大佬帮忙讲解一下代码。 |