-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsame_tree.cpp
More file actions
41 lines (36 loc) · 1.21 KB
/
same_tree.cpp
File metadata and controls
41 lines (36 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// 100. Same Tree: https://leetcode.com/problems/same-tree/description/
// easy, binary tree traversal, recursion
/*
Given two binary trees root,
we have to find if those are smae stucturally or not
both trees can be same if and only if
- present node values are same
- left child node values are same
- right child node values are same
*/
/**
* 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 ; if both null then true
if(p == NULL || q == NULL) return p == q;
/* criterias to be identical:
a. node values are equal
b. node -> left values are equal
c. node -> right values are equal
*/
return (p->val == q->val) && isSameTree(p->right, q->right) && isSameTree(p->left, q->left);
}
};
// TC -> O(n) as traversing the whole trees
// SC -> O(1) as storing nothing