Skip to content

Practice Operations on Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1993
LeetCode Problem

Operations on Tree

Problem Statement

LockingTree(parent) starts with every node unlocked. lock(num,user) succeeds only if num is unlocked. unlock succeeds only for its lock owner. upgrade succeeds only if num is unlocked, has a locked descendant, and has no locked ancestor; it locks num for user and unlocks all descendants regardless of owner. Each method returns success. Ordinary lock does not check ancestors or descendants. Practice approach: Store owner per node, parent pointers, and child lists. For upgrade, verify the ancestor chain and collect locked descendants before mutating anything. Failed operations must not partially unlock nodes. The bounded tree permits O(n) upgrade traversal.

Source: Operations on Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["LockingTree","lock","unlock","upgrade","unlock","lock","upgrade"]
[[[-1,0,0,1]],[3,4],[3,5],[1,7],[3,4],[3,4],[3,9]]
Output
[null,true,false,true,false,true,false]
Explanation

Upgrade locks node 1 and clears node 3. Ordinary lock can relock node 3 despite its locked ancestor, but upgrading node 3 then fails.

Constraints

2 <= parent.length <= 2000
parent describes a tree rooted at zero, with parent[0] = -1.
0 <= num < parent.length; 1 <= user <= 10^4
At most 2000 total calls.

Related LeetCode Problems

Practice more problems similar to Operations on Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.