-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestAlg.js
More file actions
381 lines (336 loc) · 10 KB
/
testAlg.js
File metadata and controls
381 lines (336 loc) · 10 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/****
*
* 此方法用于函数测试
*
*
*/
const arrayToTreeNode = require('./tools/treeNode');
// const arrayToLinkNode = require('./tools/LinkNode');
// const linkToArray = require('./tools/LinkNodetoArray');
// function TreeDepth(pRoot) {
// // write code here
// if (pRoot !== null) {
// return Math.max(TreeDepth(pRoot.left), TreeDepth(pRoot.right)) + 1;
// } else {
// return 0;
// }
// }
// // 非递归解法
// // 层次遍历法
// function treeDep(pRoot) {
// if (pRoot === null) {
// return 0;
// }
// var treearr = [];
// treearr.push(pRoot);
// var count = 0;
// while (treearr.length !== 0) {
// count++;
// let thisLen = treearr.length;
// for (let i = 0; i < thisLen; i++) {
// var temp = treearr[0];
// treearr = treearr.slice(1);
// if (temp.left !== null) {
// treearr.push(temp.left);
// }
// if (temp.right !== null) {
// treearr.push(temp.right);
// }
// }
// }
// return count;
// }
// function IsBalanced_Solution(pRoot) {
// // write code here
// if (pRoot === null) {
// return true;
// }
// this.getDepth = function (rootNode) {
// if (rootNode === null) {
// return 0;
// }
// let left = this.getDepth(rootNode.left);
// if (left === -1) {
// return -1;
// }
// let right = this.getDepth(rootNode.right);
// if (right === -1) {
// return -1;
// }
// return Math.abs(left - right) > 1 ? -1 : Math.max(left, right) + 1;
// }
// return this.getDepth(pRoot) !== -1;
// }
// function TreeNode(x) {
// this.val = x;
// this.left = null;
// this.right = null;
// }
// function HasSubtree(pRoot1, pRoot2)
// {
// // write code here
// if(pRoot1 === null || pRoot2 === null){
// return false;
// }
// var tempState = false;
// this.checkIfhastree2 = function(tree1, tree2){
// if (tree2 === null) {
// return true;
// }
// if (tree1 === null) {
// return false;
// }
// if (tree1.val !== tree2.val ) {
// return false;
// }
// return this.checkIfhastree2(tree1.left, tree2.left) && this.checkIfhastree2(tree1.right, tree2.right);
// }
// if (pRoot1.val === pRoot2.val) {
// tempState = this.checkIfhastree2(pRoot1, pRoot2);
// }
// if (!tempState) {
// tempState = HasSubtree(pRoot1.left, pRoot2);
// }
// if (!tempState) {
// tempState = HasSubtree(pRoot1.right, pRoot2);
// }
// return tempState;
// }
// function TreeNode(x) {
// this.val = x;
// this.left = null;
// this.right = null;
// }
// 考虑使用层次遍历法
// function PrintFromTopToBottom(root) {
// // write code here
// if (root === null) {
// return [];
// }
// let tempList = [];
// let outprint = [];
// tempList.push(root);
// while (tempList.length !== 0) {
// let tempNode = tempList[0];
// outprint.push(tempNode.val);
// tempList = tempList.slice(1);
// if (tempNode.left !== null) {
// tempList.push(tempNode.left);
// }
// if (tempNode.right !== null) {
// tempList.push(tempNode.right);
// }
// }
// return outprint;
// }
// function ListNode(x) {
// this.val = x;
// this.next = null;
// }
// function deleteDuplication(pHead) {
// if (pHead === null) {
// return null;
// }
// let backLink = pHead;
// let sameVal = [];
// let current = pHead;
// // 找出链表值重复的节点的值
// while (current.next) {
// let currVal = current.val;
// current = current.next;
// if (currVal == current.val && sameVal.indexOf(currVal) === -1) {
// sameVal.push(currVal);
// }
// }
// // 删除重复数组里的值的链表节点
// let position = 0;
// let backHead;
// while (backLink) {
// let currNode;
// if (sameVal.indexOf(backLink.val) === -1) {
// if (position === 0) {
// backHead = new ListNode(backLink.val);
// currNode = backHead;
// position++;
// } else {
// currNode = backHead;
// while (currNode.next) {
// currNode = currNode.next;
// }
// currNode.next = new ListNode(backLink.val);
// }
// }
// backLink = backLink.next;
// }
// return backHead;
// }
// function TreeNode(x) {
// this.val = x;
// this.left = null;
// this.right = null;
// }
// function Print(pRoot)
// {
// let outPrint = [];
// if (pRoot === null){
// return outPrint;
// }
// let oddLine = [];
// let evenLine = [];
// oddLine.push(pRoot);
// let currentOdd = true;
// while(oddLine.length !== 0 || evenLine.length !== 0){
// if (currentOdd){
// let tempArr = [];
// while(oddLine.length !== 0){
// let tempNode = oddLine[0];
// oddLine = oddLine.slice(1);
// if (tempNode !== null) {
// tempArr.push(tempNode.val);
// if (tempNode.left !== null){
// evenLine.push(tempNode.left);
// }
// if (tempNode.right !== null){
// evenLine.push(tempNode.right);
// }
// }
// }
// if (tempArr.length !== 0){
// outPrint.push(tempArr);
// }
// } else {
// let tempArr = [];
// while(evenLine.length !== 0){
// let tempNode = evenLine[0];
// evenLine = evenLine.slice(1);
// if (tempNode !== null) {
// tempArr.push(tempNode.val);
// if (tempNode.left !== null){
// oddLine.push(tempNode.left);
// }
// if (tempNode.right !== null){
// oddLine.push(tempNode.right);
// }
// }
// }
// if (tempArr.length !== 0){
// outPrint.push(tempArr);
// }
// }
// currentOdd = !currentOdd;
// }
// return outPrint;
// }
// function TreeNode(x) {
// this.val = x;
// this.left = null;
// this.right = null;
// }
// function isSymmetrical(pRoot)
// {
// let symmetrical = false;
// if (pRoot === null) {
// return symmetrical;
// }
// this.compareNode = function(leftNode, rightNode){
// if (leftNode === null){
// return rightNode === null;
// }
// if (rightNode === null){
// return false;
// }
// if (leftNode.val !== rightNode.val){
// return false;
// }
// return this.compareNode(leftNode.left, rightNode.right) && this.compareNode(leftNode.right, rightNode.left);
// }
// return this.compareNode(pRoot.left, pRoot.right);
// }
// function Serialize(pRoot) {
// // 使用前序遍历的形式访问整个二叉树
// let nodeStr = '';
// if (pRoot === null) {
// nodeStr += '@,';
// return nodeStr;
// }
// nodeStr += pRoot.val + ',';
// nodeStr += Serialize(pRoot.left);
// nodeStr += Serialize(pRoot.right);
// return nodeStr;
// }
// let index = -1;
// function Deserialize(s) {
// index++;
// if (index >= s.length) {
// return null;
// }
// let nodeArr = s.split(',');
// let treeNode = null;
// if (nodeArr[index] !== '@') {
// treeNode = new TreeNode(nodeArr[index]);
// treeNode.left = Deserialize(s);
// treeNode.right = Deserialize(s);
// }
// return treeNode;
// }
function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
}
function FindPath(root, expectNumber) {
let pathArr = [];
// 考虑使用 dfs 完成路径得到搜索
this.dfs = function (tempRoot, expectNumber, currentNum, currentArr, pathArr) {
if (tempRoot === null) {
return;
}
if (currentNum + tempRoot.val === expectNumber) {
currentArr.push(tempRoot);
pathArr.push(currentArr);
return;
} else if (currentNum + tempRoot.val < expectNumber) {
currentArr.push(tempRoot);
currentNum += tempRoot.val;
this.dfs(tempRoot.left, expectNumber, currentNum, currentArr, pathArr);
this.dfs(tempRoot.right, expectNumber, currentNum, currentArr, pathArr);
} else {
return;
}
}
let currentNode = root;
let nodeArrays = [];
nodeArrays.push(currentNode);
while (nodeArrays.length !== 0) {
currentNode = nodeArrays[0];
nodeArrays = nodeArrays.slice(1);
if (currentNode.left !== null) {
nodeArrays.push(currentNode.left);
}
if (currentNode.right !== null) {
nodeArrays.push(currentNode.right);
}
this.dfs(currentNode, expectNumber, 0, [], pathArr);
}
pathArr.sort((a, b) => {
return b.length - a.length;
})
return pathArr;
}
// let arr1 = [1, 1, 2, 3, 3, 4, 5];
let arr1 = [5,6,7,8,7,6,5];
let arr2 = [5, 4, "#", 3, "#", 2];
// var tempLink1 = arrayToLinkNode(arr1);
// var tempLink2 = arrayToTreeNode(arr2);
var tempTree = arrayToTreeNode(arr1);
console.log(tempTree);
let result = FindPath(tempTree, );
console.log(result);
console.log(Deserialize(result));
// console.log(tempLink2);
// var list1 = linkToArray(tempLink1);
// console.log(list1);
// var list2 = PrintFromTopToBottom(tempLink2);
// console.log(list2);
// console.log('递归版本是否平衡二叉树:', IsBalanced_Solution(tempTree));
// console.log('非递归版本深度:', treeDep(tempTree));