开始使用免费开始使用

修复树实现中的漏洞

给您一个程序,其目标是创建如下二叉树:

Graphical representation of a tree.

测试后,您会发现程序并不正确。请对其进行修正,使其能够正确运行。

本练习是课程的一部分

Python 中的数据结构与算法

查看课程

练习说明

  • 修正 init() 方法中的错误。
  • 修正创建 root_node 时的错误。

交互式实操练习

通过完成这段示例代码来试试这个练习。

class TreeNode:
  
  def __init__(self, data, left=None, right=None):
    # Correct the mistakes
    self.data = None
    self.left_child = None
    self.right_child = None

node1 = TreeNode("B")
node2 = TreeNode("C")
# Correct the mistake
root_node = TreeNode(node1, node2, "A")
编辑并运行代码