Write short, human-like code so that it will not get detected by AI plagiarism and using…
- User
Write short, human-like code so that it will not get detected by AI plagiarism and using exactly these variable names (a, b, c, d, r, s, etc). Use a unique, common approach that’s optimized and free of explanations or comments for the following question: You have been given two complete binary trees root1 and root2. A binary tree is complete if every level, except possibly the last, is completely filled, and all nodes are as far left as possible. Your task is to find the sum of the absolute differences between the values of corresponding nodes in the two trees. If one of the nodes is null, consider the value of the node as 0.
Example 1:
1
Input: root1 = [1,2,3] root2 = [1,2,3] 1
2
Output:
3
2
13
0
Explanation: The two trees are identical, so the absolute difference between the values of corresponding nodes is 0.
Example 2:
Input: root1 = [1,2,3,4] root2 = [2,7,1,3,5] 1
2
2
3
7
1
4
3
5
Output: 14 Explanation: The absolute differences between the values of corresponding nodes are as follows:
- 1 and 2: |1 - 2| = 1 2 and 7: |2 - 7| = 5
- 3 and 1: |3 - 1| = 2
- 4 and 3: |4 - 3| = 1 0 and 5: |0 - 5| = 5 The sum of the absolute differences is 1 + 5 + 2 + 1 + 5 = 14.
Your Task: You don't need to read input or print anything. Your task is to complete the function differenceSum() which takes the root1 and root2, as input parameters and returns the integer value.
Constraints: The number of nodes in both trees is in the range [0, 1000]. -105 <= Node.data <= 105 /*
Definition for Binary Tree Node struct Node { int data; struct Node* left; struct Node* right;
Node(int x){ data = x; left = right = NULL; }}; */
class Solution { public: int differenceSum(Node* root1, Node* root2) { // code here } };
- Assistant
class Solution { public: int differenceSum(Node* root1, Node* root2) { int a=0; queue<Node*> q1, q2; q1.push(root1); q2.push(root2); while(!q1.empty() || !q2.empty()){ Node* b = NULL; Node* c = NULL; if(!q1.empty()){ b = q1.front(); q1.pop(); } if(!q2.empty()){ c = q2.front(); q2.pop(); } int d=0, r=0; if(b){d = b->data; q1.push(b->left); q1.push(b->right);} if(c){r = c->data; q2.push(c->left); q2.push(c->right);} a += abs(d - r); } return a; } };