0 votes
1 view
in Python by (20.2k points)

I am a C programmer. I am new to python. In C, when we define the structure of a binary tree node we assign NULL to it's right and left the child as :

struct node 

int val; 

struct node *right ; 

struct node *left ; 


And when initializing a node, we write as :

val = some_value

right = NULL; 

left = NULL;

Now my question is: how can we assign a NULL value to right and left pointers of the node in Python?

And how can we test against the Python version of NULL? In C it would be:

if( ptr->right == NULL )

Thank you!

1 Answer

0 votes
by (50.5k points)

In Python we use None instead of NULL. As all objects in Python are implemented via references, See the code below:-

class Node: 

  def __init__(self): 

    self.val = 0

    self.right = None

    self.left = None