Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the following tree node definition TreeNode {TreeNode^* left, ^*right; Int v

ID: 3581847 • Letter: U

Question

Use the following tree node definition TreeNode {TreeNode^* left, ^*right; Int val;} Given two binary trees, write a function to check if they are identical or not. By "identical", we mean two trees have exactly the same structure and key values. Void identical (TreeNode^*T1, TreeNode^* T2) ...//your implementation Given a node in binary search tree (BST) say x, design an algorithm to find the in-order successor of node x is the node with the smallest value greater than node xs value. Note that there is no link pointing back to a node's parent. An example BST is given for your reference.

Explanation / Answer

Here is the code for the first problem:

TreeNode
{
TreeNode* left, *right;
int val;
}
void identical(TreeNode *T1, TreeNode * T2)
{
if(identicalHelper(T1, T2))
printf("The trees are identical. ");
else
printf("The trees are NOT identical. ");
}
int identicalHelper(TreeNode *T1, TreeNode *T2)
{
if(T1 == NULL && T2 == NULL)   //If both subtrees are empty.
return 1;                   //Return true.
else if(T1 == NULL || T2 == NULL)   //If one tree is empty and the other is not.
return 0;                    //Return false.
else                           //If no tree is empty.
{
if(T1->item != T2->item)   //If the values at that node for each tree are not same.
return 0;                   //Return false.
//If left tree of both trees are identical, and right tree of both trees are identical, return accordingly.
return identicalHelper(T1->left, T2->left) && identicalHelper(T1->right, T2->right);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote