|
1 | 1 | # [1993.Operations on Tree][title]
|
2 | 2 |
|
3 |
| -> [!WARNING|style:flat] |
4 |
| -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) |
5 |
| -
|
6 | 3 | ## Description
|
| 4 | +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 i<sup>th</sup> 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. |
7 | 5 |
|
8 |
| -**Example 1:** |
| 6 | +The data structure should support the following functions: |
9 | 7 |
|
10 |
| -``` |
11 |
| -Input: a = "11", b = "1" |
12 |
| -Output: "100" |
13 |
| -``` |
| 8 | +- **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. |
| 9 | +- **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. |
| 10 | +- **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: |
14 | 11 |
|
15 |
| -## 题意 |
16 |
| -> ... |
| 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. |
17 | 15 |
|
18 |
| -## 题解 |
| 16 | +Implement the `LockingTree` class: |
19 | 17 |
|
20 |
| -### 思路1 |
21 |
| -> ... |
22 |
| -Operations on Tree |
23 |
| -```go |
24 |
| -``` |
| 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**. |
25 | 22 |
|
| 23 | +**Example 1:** |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +``` |
| 28 | +Input |
| 29 | +["LockingTree", "lock", "unlock", "unlock", "lock", "upgrade", "lock"] |
| 30 | +[[[-1, 0, 0, 1, 1, 2, 2]], [2, 2], [2, 3], [2, 2], [4, 5], [0, 1], [0, 1]] |
| 31 | +Output |
| 32 | +[null, true, false, true, true, true, false] |
| 33 | +
|
| 34 | +Explanation |
| 35 | +LockingTree lockingTree = new LockingTree([-1, 0, 0, 1, 1, 2, 2]); |
| 36 | +lockingTree.lock(2, 2); // return true because node 2 is unlocked. |
| 37 | + // Node 2 will now be locked by user 2. |
| 38 | +lockingTree.unlock(2, 3); // return false because user 3 cannot unlock a node locked by user 2. |
| 39 | +lockingTree.unlock(2, 2); // return true because node 2 was previously locked by user 2. |
| 40 | + // Node 2 will now be unlocked. |
| 41 | +lockingTree.lock(4, 5); // return true because node 4 is unlocked. |
| 42 | + // Node 4 will now be locked by user 5. |
| 43 | +lockingTree.upgrade(0, 1); // return true because node 0 is unlocked and has at least one locked descendant (node 4). |
| 44 | + // Node 0 will now be locked by user 1 and node 4 will now be unlocked. |
| 45 | +lockingTree.lock(0, 1); // return false because node 0 is already locked. |
| 46 | +``` |
26 | 47 |
|
27 | 48 | ## 结语
|
28 | 49 |
|
|
0 commit comments