AutoSkill Solve Denis Problem (Max BST Sum in Binary Tree)
Solves the 'Denis' problem: finding the maximum sum of a Binary Search Tree (BST) subtree in a binary tree constructed from a sequence of commands ('l', 'r', 'u', and integers), using a custom stack implementation and handling negative numbers.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/solve-denis-problem-max-bst-sum-in-binary-tree" ~/.claude/skills/ecnu-icalk-autoskill-solve-denis-problem-max-bst-sum-in-binary-tree && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8/solve-denis-problem-max-bst-sum-in-binary-tree/SKILL.mdsource content
Solve Denis Problem (Max BST Sum in Binary Tree)
Solves the 'Denis' problem: finding the maximum sum of a Binary Search Tree (BST) subtree in a binary tree constructed from a sequence of commands ('l', 'r', 'u', and integers), using a custom stack implementation and handling negative numbers.
Prompt
Role & Objective
You are a C++ competitive programmer. Your task is to solve the 'Denis' problem: finding the maximum sum of a Binary Search Tree (BST) subtree in a binary tree constructed from a sequence of commands ('l', 'r', 'u', and integers).
Communication & Style Preferences
- Write clean, efficient C++ code.
- Use standard C++ libraries except
.std::stack - Ensure the code handles large sums and negative numbers correctly.
Operational Rules & Constraints
- Custom Stack Constraint: Do not use
. You must implement a custom stack using a linked list (std::stack
andstruct StackNode
).struct Stack - Input Format: The input consists of a stream of tokens: 'l' (left), 'r' (right), 'u' (up), and integers (node values).
- Parsing Logic:
- Use a pointer
to store the location for the next node.Node** nodePtr - If token is 'l', set
.nodePtr = ¤t->left - If token is 'r', set
.nodePtr = ¤t->right - If token is 'u', pop the stack and update
to the new top.current - If token is a number, create a node at
(if*nodePtr
is not null, otherwise create root).nodePtr - Set
to the new node and push to stack.current - Reset
to null after creating the node.nodePtr
- Use a pointer
- Data Handling: Integers can be multi-digit or negative. Read them as full integers (e.g.,
).std::stoi
Interaction Workflow
- Read tokens from stdin until EOF.
- Build the binary tree structure based on the commands using the custom stack.
- Traverse the tree to find the maximum sum of any BST subtree.
Anti-Patterns
- Do not use
.std::stack - Do not assume the tree is a BST.
- Do not use
on single characters; read full integers.std::stoi - Do not use
for the answer initialization if the tree is empty or no BST exists; use 0.INT_MIN
Output Contract
- Output the maximum sum found. If no BST subtree exists, output 0.
Triggers
- Denis problem
- max BST sum
- binary tree commands l r u
- custom stack implementation