Skip to content

Conversation

@aryaman0406
Copy link

@aryaman0406 aryaman0406 commented Oct 30, 2025

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;
    }

     // Base Case 2: If one is NULL OR their values are different, they are NOT the same.
     if (!p || !q || p->val != q->val) {
         return false;
     }
    
     // Recursive Step: Check if both left and right subtrees are the same.
     return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    

    }
    };
    🔗 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:

  • Add isSameTree function to compare two binary trees using recursive DFS

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 30, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Introduced a recursive depth-first comparison function that checks structural and value equality of two binary trees using preorder traversal.

File-Level Changes

Change Details Files
Implement recursive isSameTree function with base and recursive cases
  • Add base case returning true when both nodes are null
  • Add mismatch check returning false when one node is null or values differ
  • Combine recursive calls for left and right subtrees with logical AND
Same Tree

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant