Operations on Tree - Problem
Imagine you're building a hierarchical access control system for a corporate network! You have a tree structure with n nodes numbered from 0 to n-1, where each node represents a resource in the system.
Given a parent array where parent[i] is the parent of the i-th node, you need to design a data structure that supports three critical operations:
- Lock: ๐ Lock a node for a specific user (only if currently unlocked)
- Unlock: ๐ Unlock a node (only by the same user who locked it)
- Upgrade: โฌ๏ธ Lock a node and unlock ALL its descendants, but only if: the node is unlocked, has locked descendants, and no locked ancestors
The root is always node 0 with parent[0] = -1. Your task is to implement the LockingTree class that efficiently handles these operations while maintaining the tree's access control rules.
Input & Output
example_1.py โ Basic Operations
$
Input:
parent = [-1, 0, 0, 1, 1, 2, 2]
Operations: lock(2, 2), unlock(2, 3), unlock(2, 2), lock(4, 5), upgrade(0, 1), lock(0, 1)
โบ
Output:
[true, false, true, true, true, false]
๐ก Note:
Lock node 2 for user 2 (success), unlock node 2 by user 3 (fail - wrong user), unlock by user 2 (success), lock node 4 for user 5 (success), upgrade node 0 for user 1 (success - unlocks node 4), lock node 0 for user 1 (fail - already locked)
example_2.py โ Upgrade with Ancestors
$
Input:
parent = [-1, 0, 1, 1, 2, 2]
Operations: lock(2, 2), lock(3, 3), upgrade(0, 1), upgrade(1, 1)
โบ
Output:
[true, true, true, false]
๐ก Note:
Lock nodes 2 and 3, upgrade root (success - unlocks both), then try upgrade node 1 (fail - no locked descendants after previous upgrade)
example_3.py โ Complex Hierarchy
$
Input:
parent = [-1, 0, 0, 1, 1, 2, 2, 3, 3]
Operations: lock(4, 1), lock(7, 2), upgrade(1, 3), lock(2, 4), upgrade(0, 5)
โบ
Output:
[true, true, true, true, false]
๐ก Note:
Lock leaf nodes, upgrade middle node (success), lock another middle node, then fail to upgrade root because node 1 is locked (locked ancestor check)
Constraints
- n == parent.length
- 2 โค n โค 2000
- 0 โค parent[i] โค n - 1 for i โ 0
- parent[0] == -1
- The parent array represents a valid tree
- At most 2000 calls in total will be made to lock, unlock, and upgrade
Visualization
Tap to expand
Understanding the Visualization
1
Department Hierarchy
The tree represents organizational structure with parent-child relationships between departments
2
Access Control Rules
Users can lock departments they have access to, but can't override their superiors
3
Upgrade Authorization
Upgrading gives exclusive access but requires: no boss interference, unlocked target, and locked subordinates
4
Efficient Implementation
Preprocessing the hierarchy enables fast permission checks and batch operations
Key Takeaway
๐ฏ Key Insight: Preprocessing the tree structure during initialization enables O(1) lock/unlock operations and efficient upgrade operations by caching parent-child relationships and using targeted DFS traversal.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code