1993. Operations on Tree
https://leetcode.com/problems/operations-on-tree
Description
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 ith
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.
The data structure should support the following functions:
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.
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.
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:
The node is unlocked,
It has at least one locked descendant (by any user), and
It does not have any locked ancestors.
Implement the LockingTree
class:
LockingTree(int[] parent)
initializes the data structure with the parent array.lock(int num, int user)
returnstrue
if it is possible for the user with iduser
to lock the nodenum
, orfalse
otherwise. If it is possible, the nodenum
will become locked by the user with iduser
.unlock(int num, int user)
returnstrue
if it is possible for the user with iduser
to unlock the nodenum
, orfalse
otherwise. If it is possible, the nodenum
will become unlocked.upgrade(int num, int user)
returnstrue
if it is possible for the user with iduser
to upgrade the nodenum
, orfalse
otherwise. If it is possible, the nodenum
will be upgraded.
Example 1:
Constraints:
n == parent.length
2 <= n <= 2000
0 <= parent[i] <= n - 1
fori != 0
parent[0] == -1
0 <= num <= n - 1
1 <= user <= 104
parent
represents a valid tree.At most
2000
calls in total will be made tolock
,unlock
, andupgrade
.
ac
Last updated