1993. Operations on Tree Medium

@problem@discussion
#Hash Table#Tree#Depth-First Search#Breadth-First Search#Design



1/**
2 * [1993] Operations on Tree
3 *
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^th 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.
5 * The data structure should support the following functions:
6 * 
7 * 	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.
8 * 	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.
9 * 	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:
10 * 	
11 * 		The node is unlocked,
12 * 		It has at least one locked descendant (by any user), and
13 * 		It does not have any locked ancestors.
14 * 	
15 * 	
16 * 
17 * Implement the LockingTree class:
18 * 
19 * 	LockingTree(int[] parent) initializes the data structure with the parent array.
20 * 	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.
21 * 	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.
22 * 	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.
23 * 
24 *  
25 * Example 1:
26 * <img alt="" src="https://assets.leetcode.com/uploads/2021/07/29/untitled.png" style="width: 375px; height: 246px;" />
27 * Input
28 * ["LockingTree", "lock", "unlock", "unlock", "lock", "upgrade", "lock"]
29 * [[[-1, 0, 0, 1, 1, 2, 2]], [2, 2], [2, 3], [2, 2], [4, 5], [0, 1], [0, 1]]
30 * Output
31 * [null, true, false, true, true, true, false]
32 * Explanation
33 * LockingTree lockingTree = new LockingTree([-1, 0, 0, 1, 1, 2, 2]);
34 * lockingTree.lock(2, 2);    // return true because node 2 is unlocked.
35 *                            // Node 2 will now be locked by user 2.
36 * lockingTree.unlock(2, 3);  // return false because user 3 cannot unlock a node locked by user 2.
37 * lockingTree.unlock(2, 2);  // return true because node 2 was previously locked by user 2.
38 *                            // Node 2 will now be unlocked.
39 * lockingTree.lock(4, 5);    // return true because node 4 is unlocked.
40 *                            // Node 4 will now be locked by user 5.
41 * lockingTree.upgrade(0, 1); // return true because node 0 is unlocked and has at least one locked descendant (node 4).
42 *                            // Node 0 will now be locked by user 1 and node 4 will now be unlocked.
43 * lockingTree.lock(0, 1);    // return false because node 0 is already locked.
44 * 
45 *  
46 * Constraints:
47 * 
48 * 	n == parent.length
49 * 	2 <= n <= 2000
50 * 	0 <= parent[i] <= n - 1 for i != 0
51 * 	parent[0] == -1
52 * 	0 <= num <= n - 1
53 * 	1 <= user <= 10^4
54 * 	parent represents a valid tree.
55 * 	At most 2000 calls in total will be made to lock, unlock, and upgrade.
56 * 
57 */
58pub struct Solution {}
59
60// problem: https://leetcode.com/problems/operations-on-tree/
61// discuss: https://leetcode.com/problems/operations-on-tree/discuss/?currentPage=1&orderBy=most_votes&query=
62
63// submission codes start here
64
65struct LockingTree {
66        false
67    }
68
69
70/** 
71 * `&self` means the method takes an immutable reference.
72 * If you need a mutable reference, change it to `&mut self` instead.
73 */
74impl LockingTree {
75
76    fn new(parent: Vec<i32>) -> Self {
77        
78    }
79    
80    fn lock(&self, num: i32, user: i32) -> bool {
81        
82    }
83    
84    fn unlock(&self, num: i32, user: i32) -> bool {
85        
86    }
87    
88    fn upgrade(&self, num: i32, user: i32) -> bool {
89        
90    }
91}
92
93/**
94 * Your LockingTree object will be instantiated and called as such:
95 * let obj = LockingTree::new(parent);
96 * let ret_1: bool = obj.lock(num, user);
97 * let ret_2: bool = obj.unlock(num, user);
98 * let ret_3: bool = obj.upgrade(num, user);
99 */
100
101// submission codes end
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn test_1993() {
109    }
110}
111


Back
© 2025 bowen.ge All Rights Reserved.