</>

100. Same Tree

Easy NeetCode 150
Trees Time O(N) Space O(H)
↗ Open on LeetCode Source: Misc

Quick review

Pattern
Trees
Time
O(N)
Space
O(H)

Standard Solution (C++)

SummaryGiven two binary trees, determine whether they are structurally identical and have the same node values at every corresponding position.

Approach
  • Recursively compare both trees in a single DFS pass using the natural base cases: two null pointers are equal; one null and one non-null are not.
  • At each node, check that the current values match, then require both the left subtrees and the right subtrees to also be identical.
  • This works because tree equality is fully decomposable — a tree equals another if and only if the roots match and every subtree pair matches.
  • Short-circuit evaluation (&&) ensures we stop as soon as any mismatch is found.
#include <cstddef>
using namespace std;

// 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) {
        // Both null: structurally identical at this position.
        if (!p && !q) return true;
        // Exactly one null: structures diverge.
        if (!p || !q) return false;
        // Values must match, then recurse on both subtrees.
        return p->val == q->val
            && isSameTree(p->left,  q->left)
            && isSameTree(p->right, q->right);
    }
};
Time O(n) Space O(h), where h is the height of the tree (O(log n) balanced, O(n) worst-case skewed)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (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) {
        if(!p){
            return !q;
        }
        else if(!q){
            return !p;
        }
        else{
            return (p->val==q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
        }
    }
};

// Time Complexity: O(N)
// Space Complexity: O(H) where H is the height of the tree, since we are using recursion (the call stack will take up space). 
// In the worst case (skewed tree), H = N. In the best case (balanced tree), H = log(N).
Time O(N) Space O(H)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern