Skip to content

Commit c9cb102

Browse files
authored
Merge pull request #504 from Vipullakum007/symmetric-tree-solution
Symmetric tree solution
2 parents 01eba05 + 47185aa commit c9cb102

File tree

2 files changed

+454
-0
lines changed

2 files changed

+454
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
id: same-tree
3+
title: Same Tree Solution
4+
sidebar_label: 0100 Same Tree
5+
tags:
6+
- Binary Tree
7+
- Recursion
8+
- LeetCode
9+
- Java
10+
- Python
11+
- C++
12+
description: "This is a solution to the Same Tree problem on LeetCode."
13+
---
14+
15+
## Problem Description
16+
17+
Given the roots of two binary trees `p` and `q`, write a function to check if they are the same or not.
18+
19+
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
20+
21+
### Examples
22+
23+
**Example 1:**
24+
25+
```
26+
Input: p = [1,2,3], q = [1,2,3]
27+
Output: true
28+
```
29+
30+
**Example 2:**
31+
32+
```
33+
Input: p = [1,2], q = [1,null,2]
34+
Output: false
35+
```
36+
37+
**Example 3:**
38+
39+
```
40+
Input: p = [1,2,1], q = [1,1,2]
41+
Output: false
42+
```
43+
44+
### Constraints
45+
46+
- The number of nodes in both trees is in the range `[0, 100]`.
47+
- `$-10^4 <=$ Node.val $<= 10^4$`
48+
49+
## Solution for Same Tree Problem
50+
51+
### Intuition
52+
53+
The intuition behind the solution is to recursively check if two binary trees are identical. If both trees are empty (null), they are considered identical. If only one tree is empty or the values of the current nodes are different, the trees are not identical. Otherwise, we recursively check if the left and right subtrees of both trees are identical.
54+
55+
### Approach
56+
57+
1. Check the base case: if both trees are null, return true.
58+
2. Check if only one tree is null or the values of the current nodes are different, return false.
59+
3. Recursively check if the left subtrees of both trees are identical.
60+
4. Recursively check if the right subtrees of both trees are identical.
61+
5. Return the logical AND of the results from steps 3 and 4.
62+
63+
#### Code in Different Languages
64+
65+
<Tabs>
66+
<TabItem value="JavaScript" label="JavaScript" >
67+
<SolutionAuthor name="@Vipullakum007"/>
68+
```javascript
69+
function isSameTree(p, q) {
70+
if (p === null && q === null) {
71+
return true;
72+
}
73+
if (p === null || q === null || p.val !== q.val) {
74+
return false;
75+
}
76+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
77+
}
78+
```
79+
80+
</TabItem>
81+
<TabItem value="Python" label="Python" default>
82+
83+
<SolutionAuthor name="@Vipullakum007"/>
84+
```python
85+
class Solution:
86+
def isSameTree(self, p, q):
87+
if p is None and q is None:
88+
return True
89+
if p is None or q is None or p.val != q.val:
90+
return False
91+
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
92+
```
93+
94+
</TabItem>
95+
<TabItem value="Java" label="Java">
96+
<SolutionAuthor name="@Vipullakum007"/>
97+
```java
98+
class Solution {
99+
public boolean isSameTree(TreeNode p, TreeNode q) {
100+
if (p == null && q == null) {
101+
return true;
102+
}
103+
if (p == null || q == null || p.val != q.val) {
104+
return false;
105+
}
106+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
107+
}
108+
}
109+
```
110+
111+
</TabItem>
112+
<TabItem value="C++" label="C++">
113+
<SolutionAuthor name="@Vipullakum007"/>
114+
```cpp
115+
class Solution {
116+
public:
117+
bool isSameTree(TreeNode* p, TreeNode* q) {
118+
if (p == nullptr && q == nullptr) {
119+
return true;
120+
}
121+
if (p == nullptr || q == nullptr || p->val != q->val) {
122+
return false;
123+
}
124+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
125+
}
126+
};
127+
```
128+
129+
</TabItem>
130+
</Tabs>
131+
132+
### Complexity Analysis
133+
134+
- **Time complexity**: The time complexity of the solution is O(min(N,M)), where N and M are the number of nodes in the two trees, respectively. This is because we need to visit each node once in order to compare their values.
135+
- **Space complexity**: The space complexity of the solution is O(min(H1,H2)), where H1 and H2 are the heights of the two trees, respectively. This is because the space used by the recursive stack is determined by the height of the smaller tree.
136+
137+
## References
138+
139+
- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/generate-parentheses/)
140+
- **Solution Link:** [Generate Parantheses Solution on LeetCode](https://leetcode.com/problems/generate-parentheses/solutions/5016750/easy-recursion-solution-in-c-100-beats-full-expanation-with-example/)
141+
- **Authors GeeksforGeeks Profile:** [Vipul lakum](https://leetcode.com/u/vipul_lakum_02/)
142+
143+
---

0 commit comments

Comments
 (0)