The function header used MUST BE: bool sameValueTree(TreeNode *node1, TreeNode *
ID: 3852092 • Letter: T
Question
The function header used MUST BE: bool sameValueTree(TreeNode *node1, TreeNode *node2, int *& array1, int *& array2)
The helper function used MUST BE: int sizeTree(TreeNode * node)
Write a C++ function that returns True if two trees have the same values (they are made of nodes with the same values.) It should return True if the trees have te same values or False otherwise. The function takes four arguments: treenode of tree1, treenode of tree2, and two arrays. Note that two trees can have the same values but not have the same structure. To successfully code this function up you will have to populate two arrays in ascending order with the values you find when traversing the two trees. For example: if samevalueTree0 is called on the following two trees: It should return True. However, if samevalueTree0 is called on the following two trees: It should return False. Few notes to help you out: 1) Function header should be: bool samevalueTree(TreeNode *node1, TreeNode *node2, int & array 1, int *& array2);Explanation / Answer
attaching python code, which explains the logic clearly.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# @param A : root node of tree
# @param B : root node of tree
# @return an integer
def isSameTree(self, A, B):
if A is None and B is None:
return 1
if A is not None and B is not None:
return (A.val==B.val) and self.isSameTree(A.left, B.left) and self.isSameTree(A.right, B.right)
return 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.