Tree Map
https://www.youtube.com/watch?v=G3BS3sh3D8Q
https://dreammarker.tistory.com/123
https://www.youtube.com/watch?v=vRwi_UcZGjUhttps://www.youtube.com/watch?v=vRwi_UcZGjU
아 콜백 너무 어렵다....
그저께 배운 코드 정리
let a = ['A', 'B', 'C'];
// find all permutations
// but what if u want to find where B cant sit in the middle seat?
// backtrack
let result1 = [];
function permutation(usedArray, unusedArray) {
if (usedArray.length === 3) {
return result1.push(usedArray);
}
for (let i = 0; i < unusedArray.length; i++) {
permutation(
usedArray.concat(unusedArray[i]),
unusedArray.filter((el, idx) => i !== idx)
);
}
}
permutation([], a);
console.log(result1);
let result2 = [];
function backtrackingPermutation(usedArray, unusedArray) {
if (usedArray[1] === 'A' || usedArray[1] === 'B') {
return;
}
if (usedArray.length === 3) {
return result2.push(usedArray);
}
for (let i = 0; i < unusedArray.length; i++) {
backtrackingPermutation(
usedArray.concat(unusedArray[i]),
unusedArray.filter((el, idx) => i !== idx)
);
}
}
backtrackingPermutation([], a);
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
const root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(7);
root.right.left = new TreeNode(13);
root.right.right = new TreeNode(18);
// root shape
// 10
// 5 15
// 3 7 13 18
/**
* BST의 루트 노드가 주어졌을때, L이상 R이하의 값인 모든 노드들의 합을 구하시오
*/
let sum = 0;
const solution = function (root, L, R) {
// your code here
if (root.val >= L && root.val <= R) {
sum += root.val;
}
if (root.left) {
solution(root.left, L, R);
}
if (root.right) {
solution(root.right, L, R);
}
};
let path = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
];
let count = 0;
function recursion(i, j) {
if (i === 3 && j === 3) {
count++;
return;
}
togglePiece(i, j);
if (i <= 2 && path[i + 1][j] === 0) {
recursion(i + 1, j);
}
if (j <= 2 && path[i][j + 1] === 0) {
recursion(i, j + 1);
}
if (i >= 1 && path[i - 1][j] === 0) {
recursion(i - 1, j);
}
if (j >= 1 && path[i][j - 1] === 0) {
recursion(i, j - 1);
}
togglePiece(i, j);
}
function togglePiece(i, j) {
path[i][j] = path[i][j] === 0 ? 1 : 0;
}
recursion(0, 0);