Problem solution · Java

Operations on Tree

Operations on Tree: a Java solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Direct simulation
Source
walkccc LeetCode Solutions
Length
60 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Operations on Tree, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 60 lines of Java from the credited upstream file 1993.java.
  • The implementation visibly relies on sequence storage.
  • 4 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeOperations on Tree · JavaJava
Use this to learn the idea, then write your own version.
class Node {  public List<Integer> children = new ArrayList<>();  public int lockedBy = -1;} class LockingTree {  public LockingTree(int[] parent) {    this.parent = parent;    nodes = new Node[parent.length];    for (int i = 0; i < parent.length; ++i)      nodes[i] = new Node();    for (int i = 1; i < parent.length; ++i)      nodes[parent[i]].children.add(i);  }   public boolean lock(int num, int user) {    if (nodes[num].lockedBy != -1)      return false;    nodes[num].lockedBy = user;    return true;  }   public boolean unlock(int num, int user) {    if (nodes[num].lockedBy != user)      return false;    nodes[num].lockedBy = -1;    return true;  }   public boolean upgrade(int num, int user) {    if (nodes[num].lockedBy != -1)      return false;    if (!anyLockedDescendant(num))      return false;     // Walk up the hierarchy to ensure that there are no locked ancestors.    for (int i = num; i != -1; i = parent[i])      if (nodes[i].lockedBy != -1)        return false;     unlockDescendants(num);    nodes[num].lockedBy = user;    return true;  }   private int[] parent;  private Node[] nodes;   private boolean anyLockedDescendant(int i) {    return nodes[i].lockedBy != -1 ||        nodes[i].children.stream().anyMatch(child -> anyLockedDescendant(child));  }   private void unlockDescendants(int i) {    nodes[i].lockedBy = -1;    for (final int child : nodes[i].children)      unlockDescendants(child);  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗