What is the purpose of the following function – tree structure
i had a test and i need to find out what is the purpose of the next code:
just want to know is this possible to compile and run test case with this code and this code fails when i try to submit it for other test cases
class Solution
{
private:
void helper(Node* root,vector& ans){
if(root == nullptr){
return;
}
helper(root->left,ans);
ans.push_back(root->data);
helper(root->right,ans);
}
public:
//Function to check if two trees are identical.
bool isIdentical(Node *r1, Node *r2)
{
vector ans1;
vector ans2;
helper(r1,ans1);
helper(r2,ans2);
int n = ans1.size();
int m = ans2.size();
if(n != m) return false;
for(int i = 0;i<n;i++){
if(ans1[i] != ans2[i]){
return false;
}
}
return true;
}
};
Trying to fix my code for a mirror tree exercise (c++)
The exercise is pretty straight forward
I input a file with a preorder sequence:
Tree with n-branch in C
I’m trying to make a tree with more than 2 branches by node. Here is my code :