0% found this document useful (0 votes)
0 views

Same Tree Recursion

The document outlines an algorithm to check if two binary trees are identical using a recursive approach. It details the base and recursive cases, along with a Java code implementation. The algorithm has a time complexity of O(n) and a space complexity of O(h).

Uploaded by

abhireddie65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Same Tree Recursion

The document outlines an algorithm to check if two binary trees are identical using a recursive approach. It details the base and recursive cases, along with a Java code implementation. The algorithm has a time complexity of O(n) and a space complexity of O(h).

Uploaded by

abhireddie65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Algorithm: Check if Two Binary Trees are the Same

1. **Base Case**:
- If both nodes (`p` and `q`) are `null`, return `true` (both trees are empty and identical).
- If only one of the nodes is `null`, return `false` (one tree is empty, and the other is not).
- If the values of `p` and `q` are different, return `false` (trees are not identical).

2. **Recursive Case**:
- Recursively check if the left subtrees of `p` and `q` are the same.
- Recursively check if the right subtrees of `p` and `q` are the same.
- If both the left and right subtree checks return `true`, the trees are identical.

3. **Return the Final Result**:


- Combine the results of the left and right subtree checks using a logical `AND`.

---

### Code: Recursive Solution in Java


```java
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
// Base Case: Both nodes are null
if (p == null && q == null) {
return true;
}
// Base Case: One node is null or values are different
if (p == null || q == null || p.val != q.val) {
return false;
}
// Recursive Case: Check left and right subtrees
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
```

---

### Example Walkthrough


#### Example Input:
`p = [1,2,3], q = [1,2,3]`

1. Start at root nodes (`p = 1`, `q = 1`):


- Both nodes are not `null` and have the same value.
- Check left subtree (`p.left = 2`, `q.left = 2`).
- Check right subtree (`p.right = 3`, `q.right = 3`).

2. Check left subtree (`p.left = 2`, `q.left = 2`):


- Both nodes are not `null` and have the same value.
- Check left subtree (`p.left.left = null`, `q.left.left = null`): **true**.
- Check right subtree (`p.left.right = null`, `q.left.right = null`): **true**.
- Both checks return `true`, so left subtree is identical.

3. Check right subtree (`p.right = 3`, `q.right = 3`):


- Both nodes are not `null` and have the same value.
- Check left subtree (`p.right.left = null`, `q.right.left = null`): **true**.
- Check right subtree (`p.right.right = null`, `q.right.right = null`): **true**.
- Both checks return `true`, so right subtree is identical.

4. Both left and right subtrees are identical, so return `true`.

---

### Notes to Include


- **Key Idea**: The algorithm compares the structure and node values of both trees
recursively.
- **Base Cases**:
1. Both nodes are `null` → Trees are identical.
2. One node is `null` or values are different → Trees are not identical.
- **Recursive Check**:
- Ensure both left and right subtrees are identical.
- **Time Complexity**: \(O(n)\), where \(n\) is the number of nodes in the smaller tree.
- **Space Complexity**: \(O(h)\), where \(h\) is the height of the tree, due to recursion.

This algorithm is efficient, concise, and directly solves the problem requirements.

You might also like