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
Corporate Access Control SystemEngineeringExecutive OfficeSalesFrontendBackendNA SalesEU Sales๐Ÿ”“๐Ÿ”’๐Ÿ”’Access Rules๐Ÿ”’ Lock: Secure a department๐Ÿ”“ Unlock: Release by same userโฌ†๏ธ Upgrade: Take control if:โ€ข Target is unlockedโ€ข No locked superiorsโ€ข Has locked subordinatesโ€ข Unlocks all belowOptimizationโšก Precompute hierarchy๐Ÿ“š Cache parent-child maps๐Ÿ” O(1) lock operations๐ŸŒณ Efficient tree traversal๐Ÿ’พ Memory-friendly design
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.
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
23.4K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen