Given the following code
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x),
left(left),
right(right) {}
};
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
// write you code here
}
};
// add main method and test cases to test your code
Given the roots of two binary trees p and q, write a function to
check if they are the
same or not.
Two binary trees are considered the same if they are structurally
identical, and the
nodes have the same value.
Example 1:
Input: p = [1,2,3], q = [1,2,3]
Output: true
Example 2:
Input: p = [1,2], q = [1,null,2]
Output: false
Example 3:
Input: p = [1,2,1], q = [1,1,2]
Output: false
Problem 5: (10 point) - Tree Given the following code struct TreeNoded int val: TreeNode "left: TreeNode "right: TreeNodeo: val(o), leftmullptr), right(nullptr) TreeNode(int x): val(x), leti(mliptr), right(nullptr) 0 TreeNode(int x, TreeNode "left, TreeNode *right) : val(x), left(left). right(right) class Solution public bool isSame Tree TreeNode* p, TreeNode* q) { // write you code here > add main method and test cases to test your code Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the hocles have the same value Example 1: 2 3 Tut: -11,2,37, 9-11,2,31 Output: True Example 2 2 2 Input: - (1.21. - 11,null, Output: talde Example 3: 2 2 Tnput: p - 11, 11, 1-11,1,21 Output: false
Given the following code struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr),
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Given the following code struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode() : val(0), left(nullptr),
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!