-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinaryTree.js
More file actions
137 lines (106 loc) · 2.83 KB
/
binaryTree.js
File metadata and controls
137 lines (106 loc) · 2.83 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
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
const dfsWhile = (root) => {
if (root === null) return [];
const ans = [];
const stack = [root];
while (stack.length > 0) {
// 因为pop是获取数组最后的node,
// 所有若是最左边的话,应该cur.left在后面
const cur = stack.pop();
ans.push(cur.val);
if (cur.right) stack.push(cur.right);
if (cur.left) stack.push(cur.left);
}
return ans;
}
const dfsRecursive = (root) => {
if (root === null) return [];
const leftValues = dfsRecursive(root.left);
const rightValues = dfsRecursive(root.right);
return [root.val, ...leftValues, ...rightValues];
}
const bfsWhile = (root) => {
if (root === null) return [];
const ans = []
const queue = [root];
while (queue.length > 0) {
const cur = queue.shift();
ans.push(cur.val)
if (cur.left) queue.push(cur.left);
if (cur.right) queue.push(cur.right)
}
return ans;
}
const bfsRecursive = (...nodes) => {
const cur = []
const children = []
for (let node of nodes) {
if (node === null) continue;
cur.push(node.val);
if (node.left) children.push(node.left);
if (node.right) children.push(node.right);
}
return [...cur, ...children]
}
const bfsWhileIncludes = (root, target) => {
if (root === null) return false;
const queue = [root];
while (queue.length > 0) {
const cur = queue.shift();
if (cur.val === target) return true;
if (cur.left) queue.push(cur.left);
if (cur.right) queue.push(cur.right)
}
return false;
}
const bfsRecursiveIncludes = (root, target) => {
if (root === null) return false;
if (root.val === target) return true;
return bfsRecursiveIncludes(root.left, target) || bfsRecursiveIncludes(root.right, target)
}
const dfsTreeSum = (root) => {
if (root === null) return 0;
let ans = 0
const stack = [root];
while (stack.length > 0) {
const cur = stack.pop();
ans += cur.val;
if (cur.left) stack.push(cur.left);
if (cur.right) stack.push(cur.right);
}
return ans;
}
const dfsTreeSum2 = (root) => {
if (root === null) return 0;
return root.val + dfsTreeSum2(root.left) + dfsTreeSum2(root.right);
}
const maxPathSum = (root) => {
if (root === null) return 0;
if (root.left === null && root.right === null) return root.val;
const maxChild = Math.max(maxPathSum(root.left), maxPathSum(root.right));
return root.val + maxChild;
}
const inOrder = (root) => {
if (root === null) return;
inOrder(root.left);
console.log(root.value)
inOrder(root.right);
}
const preOrder = (root) => {
if (root === null) return;
console.log(root.value)
preOrder(root.left);
preOrder(root.right);
}
const postOrder = (root) => {
if (root === null) return;
postOrder(root.left);
console.log(root.value)
postOrder(root.right);
}