如何使用Python实现链表的二叉树的前序遍历操作
首先,我们需要定义一个二叉树节点的类,包含三个属性:值(val)、左子节点(left)和右子节点(right)。
class TreeNode: def __init__(self, val=0): self.val = val self.left = None self.right = None
接下来,我们可以使用链表的方式构建一颗二叉树。我们可以定义一个链表节点的类,每个链表节点包含一个二叉树节点和下一个链表节点。
class ListNode: def __init__(self, node: TreeNode): self.node = node self.next = None
然后,我们定义一个函数将链表转换为二叉树。我们可以使用一个队列和一个栈来实现这个过程。首先将链表节点压入栈中,然后从栈中弹出每一个节点,在队列中查找该节点的左右子节点,如果存在就创建一个新的链表节点,并将其压入栈中。最后返回根节点。
def list_to_tree(head: ListNode) -> TreeNode: if not head: return None root = head.node queue = [root] stack = [head] while stack: node = stack.pop() if node.next: left_node = node.next left_tree_node = TreeNode(left_node.node.val) node.node.left = left_tree_node left_child_list = left_node while left_child_list: stack.append(left_child_list) if not left_child_list.next: break left_child_list = left_child_list.next if queue: queue_node = queue.pop(0) if queue_node.left: queue.append(queue_node.left) if queue_node.right: queue.append(queue_node.right) if node.next: right_node = node.next.next if right_node: right_tree_node = TreeNode(right_node.node.val) node.node.right = right_tree_node right_child_list = right_node while right_child_list: stack.append(right_child_list) if not right_child_list.next: break right_child_list = right_child_list.next queue.append(right_tree_node) return root
最后,我们实现二叉树的前序遍历。我们可以使用递归的方式,先遍历根节点,再遍历左子树,最后遍历右子树。
def preorder_traversal(root: TreeNode): if not root: return print(root.val) preorder_traversal(root.left) preorder_traversal(root.right)
最终的完整代码如下:
class TreeNode: def __init__(self, val=0): self.val = val self.left = None self.right = None class ListNode: def __init__(self, node: TreeNode): self.node = node self.next = None def list_to_tree(head: ListNode) -> TreeNode: if not head: return None root = head.node queue = [root] stack = [head] while stack: node = stack.pop() if node.next: left_node = node.next left_tree_node = TreeNode(left_node.node.val) node.node.left = left_tree_node left_child_list = left_node while left_child_list: stack.append(left_child_list) if not left_child_list.next: break left_child_list = left_child_list.next if queue: queue_node = queue.pop(0) if queue_node.left: queue.append(queue_node.left) if queue_node.right: queue.append(queue_node.right) if node.next: right_node = node.next.next if right_node: right_tree_node = TreeNode(right_node.node.val) node.node.right = right_tree_node right_child_list = right_node while right_child_list: stack.append(right_child_list) if not right_child_list.next: break right_child_list = right_child_list.next queue.append(right_tree_node) return root def preorder_traversal(root: TreeNode): if not root: return print(root.val) preorder_traversal(root.left) preorder_traversal(root.right) if __name__ == '__main__': head = ListNode(TreeNode(1)) head.next = ListNode(TreeNode(2)) head.next.next = ListNode(TreeNode(3)) head.next.next.next = ListNode(TreeNode(4)) head.next.next.next.next = ListNode(TreeNode(5)) head.next.next.next.next.next = ListNode(TreeNode(6)) root = list_to_tree(head) preorder_traversal(root)
输出结果为:
1 2 4 5 3 6
相关文章