MulaiMulai sekarang secara gratis

Correcting bugs in a tree implementation

You have been given a program that is supposed to create the following binary tree:

Graphical representation of a tree.

Testing it, you realize that the program is not correct. Could you correct it so that it works correctly?

Latihan ini adalah bagian dari kursus

Data Structures and Algorithms in Python

Lihat Kursus

Petunjuk latihan

  • Correct the mistakes in the init() method.
  • Correct the mistake in the creation of the root_node.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

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")
Edit dan Jalankan Kode