-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem24.js
108 lines (98 loc) · 3.07 KB
/
Problem24.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
// Problem 24
//
// This problem was asked by Google.
//
// Implement locking in a binary tree. A binary tree node can be locked or unlocked only if all of its descendants or ancestors are not locked.
//
// Design a binary tree node class with the following methods:
//
// is_locked, which returns whether the node is locked
// lock, which attempts to lock the node. If it cannot be locked, then it should return false. Otherwise, it should lock it and return true.
// unlock, which unlocks the node. If it cannot be unlocked, then it should return false. Otherwise, it should unlock it and return true.
//
// You may augment the node to add parent pointers or any other property you would like.
// You may assume the class is used in a single-threaded program, so there is no need for actual locks or mutexes.
// Each method should run in O(h), where h is the height of the tree.
//
// https://www.dailycodingproblem.com/blog/lockable-binary-trees/
//
// O(1) Time complexity for isLocked and O(h) Time complexity for lock and unlock
// O(1) Space compleixty for all of the three methods
class LockingTreeNode {
/**
* Initialize a binary tree node with a value and/or left and right nodes
* @param {*} val The value stored in the binary tree node.
*/
constructor(val, left = null, right = null) {
this.val = val;
this.left = left;
this.right = right;
this.isLocked = false;
this.parent = null;
this.lockedDescendantCount = 0;
}
}
/**
* Checks to see if we can lock or unlock this node by checking ancestors and descendants
* @param {LockingTreeNode} node
* @return {boolean}
*/
function canLockOrUnlock(node) {
if (node.lockedDescendantCount > 0) return false;
let curr = node.parent;
while (curr !== null) {
if (curr.isLocked) return false;
curr = curr.parent;
}
return true;
}
/**
* Return whether the node is locked
* @param {LockingTreeNode} node
* @return {boolean}
*/
function isLocked(node) {
return node.isLocked;
}
/**
* Tries to lock the node. If it cannot be locked, then it should return false.
* Otherwise, it should lock it and return true.
* @param {LockingTreeNode} node
* @return {boolean}
*/
function lock(node) {
if (node === null) return false;
if (!node.isLocked && canLockOrUnlock(node)) {
// Not locked, so update isLocked and increment count in all ancestors
node.isLocked = true;
let curr = node.parent;
while (curr !== null) {
curr.lockedDescendantCount += 1;
curr = curr.parent;
}
return true;
}
return false;
}
/**
* Unlocks the node.
* If it cannot be unlocked, then it should return false.
* Otherwise, it should unlock it and return true.
* @param {LockingTreeNode} node
* @return {boolean}
*/
function unlock(node) {
if (node === null) return false;
if (node.isLocked && canLockOrUnlock(node)) {
node.isLocked = false;
// Update count in all ancestors
let curr = node.parent;
while (curr !== null) {
curr.lockedDescendantCount -= 1;
curr = curr.parent;
}
return true;
}
return false;
}
export { LockingTreeNode, isLocked, lock, unlock };