Same Tree

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

01 bool isSameTree(TreeNode *p, TreeNode *q)
02 {
03      if((p==NULL)&&(q==NULL))
04           return true;
05      if(p==NULL)
06           return false;
07      if(q==NULL)
08           return false;
09      if(p->val!=q->val)
10           return false;
11      if(isSameTree(p->left,q->left)==false)
12           return false;
13      if(isSameTree(p->right,q->right)==false)
14           return false;
15      return true;
16 }
This entry was posted in Recursive, Tree. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s