MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / upgrade

Method upgrade

javascript/1993-operations-on-tree.js:53–66  ·  view source on GitHub ↗

* * Time O(n) | Space O(n) * @param {number} num * @param {number} user * @return {boolean}

(num, user)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

checkDescendantsMethod · 0.95
checkAncestorsMethod · 0.95
unlockDescendantsMethod · 0.95

Tested by

no test coverage detected