-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleetcode-572-subtreeOfAnotherTree.js
160 lines (130 loc) · 3.72 KB
/
leetcode-572-subtreeOfAnotherTree.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} subRoot
* @return {boolean}
*/
// p: root, root
// r: boolean;
var isSubtree = function (root, subRoot) {
if (!root) return false;
if (root.val === subRoot.val && checkSub(root, subRoot)) return true;
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
};
const checkSub = (r1, r2) => {
if (!r1 && !r2) return true;
if (!r1 || !r2 || r1.val !== r2.val) return false;
return checkSub(r1.left, r2.left) && checkSub(r1.right, r2.right);
};
// find subRoot
// if found, start comparing left && right
// [l r
// [l r
// var isSubtree = function (root, subRoot) {
// const stack = [root];
// while (stack.length > 0) {
// const current = stack.pop();
// if (current.val === subRoot.val && compare(current, subRoot)) return true;
// current.left && stack.push(current.left);
// current.right && stack.push(current.right);
// }
// return false;
// };
// // // recursive O(n) O(n)
// const compare = (r1, r2) => {
// if (!r1 && !r2) return true;
// if (r1 && !r2) return false;
// if (!r1 && r2) return false;
// if (r1.val !== r2.val) return false;
// const compareLeft = compare(r1.left, r2.left);
// const compareRight = compare(r1.right, r2.right);
// return compareLeft && compareRight;
// };
// iterative
// const compare = (r1, r2) => {
// const stack1 = [r1];
// const stack2 = [r2];
// while (stack1.length > 0 && stack2.length > 0) {
// const c1 = stack1.pop();
// const c2 = stack2.pop();
// if (!c1 && !c2) continue;
// if (!c1 && c2) return false;
// if (c1 && !c2) return false;
// if (c1.val !== c2.val) return false;
// stack1.push(c1.left);
// stack1.push(c1.right);
// stack2.push(c2.left);
// stack2.push(c2.right);
// }
// return stack1.length === 0 && stack2.length === 0;
// };
class Tree {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
// const a = new Tree(3);
// const b = new Tree(4);
// const c = new Tree(5);
// const d = new Tree(1);
// const e = new Tree(2);
// const f = new Tree(0);
// a.left = b;
// a.right = c;
// b.left = d;
// b.right = e;
// e.right = f;
// const x = new Tree(4);
// const y = new Tree(1);
// const z = new Tree(2);
// x.left = y;
// x.right = z;
// const x = new Tree(4);
const a = new Tree(1);
const b = new Tree(1);
a.left = b;
const x = new Tree(1);
console.log(isSubtree(a, x));
// NOT WORKING
// var isSubtree = function (root, subRoot) {
// const stack = [root];
// while (stack.length > 0) {
// const current = stack.pop();
// if (current.val === subRoot.val && !subRoot.left && !subRoot.right)
// return true;
// if (current.val === subRoot.val) {
// const stackS = [subRoot];
// const stackC = [current];
// while (stackC.length > 0) {
// const cS = stackS.pop();
// const cC = stackC.pop();
// console.log(cS, cC);
// if (!cS && cC) return false;
// if (cS && !cC) return false;
// if (!cS && !cC) continue;
// if (cS.val !== cC.val) return false;
// // cS.left &&
// stackS.push(cS.left);
// // cS.right &&
// stackS.push(cS.right);
// // cC.left &&
// stackC.push(cC.left);
// // cC.right &&
// stackC.push(cC.right);
// }
// return true;
// }
// current.left && stack.push(current.left);
// current.right && stack.push(current.right);
// }
// return false;
// };