Implement isSameTree function to compare two trees #271
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Title Format: 100.Same Tree.cpp💡 IntuitionTo determine if two trees,$p$ and $q$ , are the same, we must check two things recursively:They must have the same structure (i.e., if one node is null, the corresponding node in the other tree must also be null).Any corresponding non-null nodes must have the same value.This naturally leads to a Depth-First Search (DFS) / Preorder Traversal approach, comparing the nodes at each step of the recursion.✍️ ApproachThe solution uses recursion to traverse both trees simultaneously, comparing the nodes at each level:Base Case 1 (Both Null): If both nodes $p$ and $q$ are NULL (meaning we've reached the end of a branch simultaneously), the trees are identical up to this point. We return true.Base Case 2 (One Null or Different Value): If $p$ and $q$ are not the same in structure or value, we return false. This covers two scenarios:One node is NULL and the other is not (structural mismatch).Both are non-null, but their values (p->val and q->val) are different.Recursive Step (Same Structure and Value): If both nodes $p$ and $q$ are non-null and have the same value (p->val == q->val), we must continue the comparison for their children. The trees are the same only if the left subtrees are the same AND the right subtrees are the same. We return the logical AND of the two recursive calls:$$\text{isSameTree}(p.\text{left}, q.\text{left}) \land \text{isSameTree}(p.\text{right}, q.\text{right})$$This recursive definition perfectly captures the requirement for identical trees.Code Solution (C++)C++/**
Definition for a binary tree node.
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) {
// Base Case 1: If both nodes are NULL, they are the same.
if (!p && !q) {
return true;
}
}
};
🔗 Related IssuesBy submitting this PR, I confirm that:[x] This is my original work not totally AI generated[x] I have tested the solution thoroughly on leetcode[x] I have maintained proper PR description format[x] This is a meaningful contribution, not spam
Summary by Sourcery
New Features: