Talentd
FleetCode
Make and follow sheets.Sign in

Minimum Operations to Stabilize a Binary Tree solution and explanation

Hard4 min read62.9% acceptanceUpdated 14 Oct 2026Premium elsewhere, free here

The problem

You are given the root of a binary tree. Each node contains an integer value representing its current state.

A binary tree is considered stable if for every node u:

  • If u.left exists, then |u.val - u.left.val| <= 1
  • If u.right exists, then |u.val - u.right.val| <= 1

In one operation, you may choose any node and increase or decrease its value by 1.

Return the minimum number of operations required to make the entire binary tree stable.

Example 1:

Input: root = [5,8,3]

Output: 3

Explanation:

  • Let u be the root node with value 5. Decrease the left child value from 8 to 6 using 2 operations. Then |u.val - u.left.val| = |5 - 6| = 1.
  • Increase the right child value from 3 to 4 using 1 operation. Then |u.val - u.right.val| = |5 - 4| = 1.
  • Thus, the answer is 2 + 1 = 3.

Example 2:

Input: root = [2,-1,-4]

Output: 5

Explanation:

  • Let u be the root node with value 2. Decrease the value of u from 2 to −2 using 4 operations.
  • Decrease the right child value from −4 to −3 using 1 operation. Then |u.val - u.right.val| = |-2 - (−3)| = 1.
  • Thus, the answer is 4 + 1 = 5.

Example 3:

Input: root = [0,-2,2,-3,null,1]

Output: 3

Explanation:

​​​​​​​​​​​​​​​​​​​​​

  • Let u be the root node with value 0. Increase the left child value from −2 to −1 using 1 operation, and decrease the right child value from 2 to 1 using 1 operation.
  • Now |u.val - u.left.val| = |0 - (−1)| = 1 and |u.val - u.right.val| = |0 - 1| = 1.
  • Let v be the left child of u, now having value −1. Increase its left child from −3 to −2 using 1 operation so that |v.val - v.left.val| = |−1 - (−2)| = 1.
  • The right subtree already satisfies the condition since |1 - 1| = 0.
  • Thus, the answer is 1 + 1 + 1 = 3.​​​​​​​

Constraints:

  • The number of nodes in the tree is in the range [1, 5 * 104].
  • -105 <= Node.val <= 105

Approach overview

Problem Overview: You get a binary tree and must count the fewest operations needed to make every node satisfy the stability condition from the statement. Each node's status depends on its subtree, so the problem reduces to computing a value per node and combining results bottom-up.

Approach 1: Brute Force Recheck (O(n^2) time, O(h) space)

For each node, recompute the required state of its subtree from scratch with a DFS, then apply the operation wherever the condition fails. Every node triggers a fresh traversal of its descendants, so the same subtree gets scanned many times. This is correct because you check the condition directly, but it wastes work on nested subtrees. Use it to confirm your reading of the rules on small trees before optimizing.

Approach 2: Memoized Subtree Evaluation (O(n) time, O(n) space)

Run one DFS and store each node's subtree result in a hash map keyed by node reference. When a parent asks for a child's result, you do a lookup instead of a new traversal. This removes the repeated work from Approach 1. The cost is an extra map with one entry per node, which matters if memory is tight.

Approach 3: Single-Pass Post-Order DFS Tree DP (O(n) time, O(h) space)

Process children before parents in one post-order traversal. Each call returns the minimum operations for its subtree plus the state the parent needs to decide whether to act. At each node, combine the two child returns in constant time, then add the operation count if the node violates the condition. Each node is visited once, and the recursion stack holds at most h frames, where h is the tree height. This is the approach you should write in an interview.

Recommended for interviews: Interviewers expect the single-pass post-order DP. The brute force shows you understand the rules, and the memoized version shows you can remove redundant work. The single-pass DP shows you can design a bottom-up state that carries exactly the information the parent needs. State the return value of your recursive function clearly before you code it, since most bugs in tree DP come from mixing up what a call returns.

The written solution for this problem is still being prepared. Try it yourself in the meantime.

Try solving it yourself

Complexity analysis

ApproachTimeSpaceWhen to Use
Brute Force RecheckO(n^2)O(h)Verifying rules on small trees
Memoized Subtree EvaluationO(n)O(n)When you want a simpler mental model and memory is not constrained
Single-Pass Post-Order DPO(n)O(h)General case and interviews

Frequently asked questions

Is Minimum Operations to Stabilize a Binary Tree easy or hard?

It is rated Hard, with a 53.1% acceptance rate on LeetCode. The difficulty comes from designing the right per-subtree return value, not from the code length. Once you define what each call returns, the implementation is short.

How to solve Minimum Operations to Stabilize a Binary Tree in O(n)?

Write a recursive DFS that returns the minimum operations for each subtree along with the state its parent needs. Recurse into the left and right children first, combine their returns in constant time, and add an operation if the current node violates the condition. Return the total from the root.

Minimum Operations to Stabilize a Binary Tree Python or Java solution?

Both languages use the same post-order recursion. In Python, define a recursive helper that returns a tuple of values for each subtree and track the global count with a nonlocal variable or a one-element list. In Java, return a small array or class from the helper and keep the operation count in a field or array.

What is the best approach for Minimum Operations to Stabilize a Binary Tree?

The best approach is a single post-order DFS that computes a result for each subtree and combines child results at the parent. Each node is visited exactly once, so the solution runs in O(n) time. It is the standard tree DP pattern for this type of problem.

What data structure is used in Minimum Operations to Stabilize a Binary Tree?

The problem uses a binary tree as the input and relies on recursion, where the call stack acts as an implicit stack of size O(h). Some solutions add a hash map for memoization, but the single-pass DP needs only constant extra state per call.

What is the time complexity of Minimum Operations to Stabilize a Binary Tree?

The optimal solution runs in O(n) time, where n is the number of nodes, because each node is processed once in post-order. A naive solution that rescans subtrees for every node takes O(n^2) time. The O(n) bound holds because each node does constant work when combining its children.

Is Minimum Operations to Stabilize a Binary Tree asked at Google, Amazon, or Meta?

This problem is not tagged with specific companies in the data available here, so check recent interview reports on your preferred platform before relying on it. Tree DP problems like this appear frequently in top tech interviews, so the post-order pattern is worth mastering regardless.

Comments

Now write it yourselfClose this page and solve Minimum Operations to Stabilize a Binary Tree in the editor, without looking.
Practise Minimum Operations to Stabilize a Binary Tree