-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12-二叉树的广度优先搜索.js
More file actions
42 lines (39 loc) · 1011 Bytes
/
12-二叉树的广度优先搜索.js
File metadata and controls
42 lines (39 loc) · 1011 Bytes
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
function Node (value) {
this.value = value;
this.left = null;
this.right = null;
}
let a = new Node('a');
let b = new Node('b');
let c = new Node('c');
let d = new Node('d');
let e = new Node('e');
let f = new Node('f');
let g = new Node('g');
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.left = f;
c.right = g;
function f1(rootList,target) {
if(rootList.length == 0 || rootList == null) return false;
for (let i = 0; i < rootList.length; i++) {
if(rootList[i] == null || rootList[i].left == null && rootList[i].right == null){
}else{
break;
}
return false;
}
var childrenList = [];
for (let i = 0; i < rootList.length; i++) {
if(rootList[i] !==null && rootList[i].value === target){
return true;
}else{
childrenList.push(rootList[i].left);
childrenList.push(rootList[i].right);
}
}
return f1(childrenList,target);
}
console.log(f1([a],'n'));