* * Time O(n) | Space O(n) * @param {number} num * @param {number} user * @return {boolean}
(num, user)
| 51 | * @return {boolean} |
| 52 | */ |
| 53 | upgrade(num, user) { |
| 54 | // lock the node for a given user and unlock all of its descendants no matter who locked it. |
| 55 | // 1. the given node should be unlocked |
| 56 | // 2. the given node should have at least one locked node descendant by anyone |
| 57 | // 3. the given node shouldn't have any locked ancestors |
| 58 | if (this.treeHash[num]) return false; |
| 59 | if (!this.checkDescendants(num)) return false; |
| 60 | if (!this.checkAncestors(num)) return false; |
| 61 | |
| 62 | // locking the given node |
| 63 | this.treeHash[num] = user; |
| 64 | this.unlockDescendants(num); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Helper method to unlock descendants |
nothing calls this directly
no test coverage detected