|
| 1 | +1993\. Operations on Tree |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +You are given a tree with `n` nodes numbered from `0` to `n - 1` in the form of a parent array `parent` where `parent[i]` is the parent of the <code>i<sup>th</sup></code> node. The root of the tree is node `0`, so `parent[0] = -1` since it has no parent. You want to design a data structure that allows users to lock, unlock, and upgrade nodes in the tree. |
| 6 | + |
| 7 | +The data structure should support the following functions: |
| 8 | + |
| 9 | +* **Lock:** **Locks** the given node for the given user and prevents other users from locking the same node. You may only lock a node using this function if the node is unlocked. |
| 10 | +* **Unlock: Unlocks** the given node for the given user. You may only unlock a node using this function if it is currently locked by the same user. |
| 11 | +* **Upgrade****: Locks** the given node for the given user and **unlocks** all of its descendants **regardless** of who locked it. You may only upgrade a node if **all** 3 conditions are true: |
| 12 | + * The node is unlocked, |
| 13 | + * It has at least one locked descendant (by **any** user), and |
| 14 | + * It does not have any locked ancestors. |
| 15 | + |
| 16 | +Implement the `LockingTree` class: |
| 17 | + |
| 18 | +* `LockingTree(int[] parent)` initializes the data structure with the parent array. |
| 19 | +* `lock(int num, int user)` returns `true` if it is possible for the user with id `user` to lock the node `num`, or `false` otherwise. If it is possible, the node `num` will become **locked** by the user with id `user`. |
| 20 | +* `unlock(int num, int user)` returns `true` if it is possible for the user with id `user` to unlock the node `num`, or `false` otherwise. If it is possible, the node `num` will become **unlocked**. |
| 21 | +* `upgrade(int num, int user)` returns `true` if it is possible for the user with id `user` to upgrade the node `num`, or `false` otherwise. If it is possible, the node `num` will be **upgraded**. |
| 22 | + |
| 23 | +**Example 1:** |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +**Input** |
| 28 | + |
| 29 | +["LockingTree", "lock", "unlock", "unlock", "lock", "upgrade", "lock"] |
| 30 | + |
| 31 | +[[[-1, 0, 0, 1, 1, 2, 2]], [2, 2], [2, 3], [2, 2], [4, 5], [0, 1], [0, 1]] |
| 32 | + |
| 33 | +**Output:** [null, true, false, true, true, true, false] |
| 34 | + |
| 35 | +**Explanation:** |
| 36 | + |
| 37 | + LockingTree lockingTree = new LockingTree([-1, 0, 0, 1, 1, 2, 2]); |
| 38 | + lockingTree.lock(2, 2); // return true because node 2 is unlocked. |
| 39 | + // Node 2 will now be locked by user 2. |
| 40 | + lockingTree.unlock(2, 3); // return false because user 3 cannot unlock a node locked by user 2. |
| 41 | + lockingTree.unlock(2, 2); // return true because node 2 was previously locked by user 2. |
| 42 | + // Node 2 will now be unlocked. |
| 43 | + lockingTree.lock(4, 5); // return true because node 4 is unlocked. |
| 44 | + // Node 4 will now be locked by user 5. |
| 45 | + lockingTree.upgrade(0, 1); // return true because node 0 is unlocked and has at least one locked descendant (node 4). |
| 46 | + // Node 0 will now be locked by user 1 and node 4 will now be unlocked. |
| 47 | + lockingTree.lock(0, 1); // return false because node 0 is already locked. |
| 48 | + |
| 49 | +**Constraints:** |
| 50 | + |
| 51 | +* `n == parent.length` |
| 52 | +* `2 <= n <= 2000` |
| 53 | +* `0 <= parent[i] <= n - 1` for `i != 0` |
| 54 | +* `parent[0] == -1` |
| 55 | +* `0 <= num <= n - 1` |
| 56 | +* <code>1 <= user <= 10<sup>4</sup></code> |
| 57 | +* `parent` represents a valid tree. |
| 58 | +* At most `2000` calls **in total** will be made to `lock`, `unlock`, and `upgrade`. |
0 commit comments