-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-043-treePathFinder.js
140 lines (109 loc) · 3.32 KB
/
structy-043-treePathFinder.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
// https://structy.net/problems/premium/tree-path-finder
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
// p: root of bi tree & val (str/num)
// r: arr / null
// // dfs recursion better //////////////////////////////////////////////////// but have no idea why it works
const pathFinder = (root, target) => {
if (!root) return null;
if (root.val === target) return [root.val];
const rightPath = pathFinder(root.right, target);
const leftPath = pathFinder(root.left, target);
if (rightPath) rightPath.unshift(root.val);
if (leftPath) leftPath.unshift(root.val);
console.log(leftPath || rightPath);
return leftPath || rightPath;
};
// ///////////////////////////// alvin's solution
// const pathFinder = (root, target) => {
// if (!root) return null;
// if (root.val === target) return [root.val];
// const rightPath = pathFinder(root.right, target);
// if (rightPath) {
// rightPath.unshift(root.val);
// return rightPath;
// }
// const leftPath = pathFinder(root.left, target);
// if (leftPath) {
// leftPath.unshift(root.val);
// return leftPath;
// }
// return null;
// };
// // dfs recusion 2 NOT WORKING
// const pathFinder = (root, target, arr = []) => {
// if (!root) return null;
// if (root.val === target) arr.push(target);
// if (pathFinder(root.right, target, arr)) {
// arr.push(root.val);
// root.val = target;
// }
// if (pathFinder(root.left, target, arr)) {
// arr.push(root.val);
// root.val = target;
// }
// // pathFinder(root.right, target, arr).includes(target) && arr.push(root.val);
// // pathFinder(root.left, target, arr).includes(target) && arr.push(root.val);
// return arr;
// };
// // dfs recursion 1 - WORKING
// const pathFinder = (root, target) => {
// const arr = [];
// explore(root, target, arr);
// return arr.length === 0 ? null : arr.reverse();
// };
// const explore = (root, target, arr) => {
// if (!root) return null;
// if (root.val === target) arr.push(target);
// if (
// explore(root.right, target, arr) === target ||
// explore(root.left, target, arr) === target
// ) {
// arr.push(root.val);
// root.val = target;
// }
// return root.val;
// };
// // dfs iteration NOT WORKING
// const pathFinder = (root, target) => {
// const stack = [root];
// const map = {};
// while (stack.length > 0) {
// const current = stack.pop();
// map[current.val] = [];
// // if (current.val === "e") return "e";
// if (current.left) {
// map[current.val].push(current.left.val);
// stack.push(current.left);
// }
// if (current.right) {
// map[current.val].push(current.right.val);
// stack.push(current.right);
// }
// // console.log(current);
// }
// return map;
// };
const a = new Node("a");
const b = new Node("b");
const c = new Node("c");
const d = new Node("d");
const e = new Node("e");
const f = new Node("f");
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.right = f;
// a
// / \
// b c
// / \ \
// d e f
console.log(pathFinder(a, "e")); // -> [ 'a', 'b', 'e' ]
// console.log(pathFinder(a, "p")); // -> null