class Node:def __init__(self, data):self.left = Noneself.right = Noneself.data = data# Insert Nodedef insert(self, data):if self.data:if data self.data:if self.right is None:self.right = Node(data)else:self.right.insert(data)else:self.data = data# Print the Treedef PrintTree(self):if self.left:self.left.PrintTreeprint( self.data),if self.right:self.right.PrintTree# 中序遍历# Left -> root -> Rightdef inorderTraversal(self, root):res = if root:res = self.inorderTraversal(root.left)res.append(root.data)res = res + self.inorderTraversal(root.right)return resroot = Node(27)root.insert(14)root.insert(35)root.insert(10)root.insert(19)root.insert(31)root.insert(42)print(root.inorderTraversal(root))摘要:class Node:def __init__(self, data):self.left = Noneself.right = Noneself.data = data# Insert Nodedef insert(self, data):if self.d
来源:小亮课堂
免责声明:本站系转载,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与本站联系,我们将在第一时间删除内容!