FastPrepBinary Tree Deletion Order
Problems/Binary Tree Deletion Order

Binary Tree Deletion Order

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks you to delete every node of a tree, then asks how to avoid risking the recursive call stack. Here we use a binary tree. The output is the deletion order: left child, right child, then parent. Our version is about 82% the same as the reported question.

Given the root of a binary tree, return the node values in a safe deletion order: every child must appear before its parent.

When both children exist, delete the entire left subtree before the right subtree, then delete the parent. Values need not be unique. Return an empty array for an empty tree.

Your final solution must avoid recursion so that a deeply skewed tree cannot overflow the call stack.

Function

deleteTreeOrder(root: TreeNode) → int[]

Examples

Example 1

root = [1,2,3,null,4]return = [4,2,3,1]
root
1243

Delete leaf 4 before its parent 2, then the right subtree rooted at 3, and finally root 1.

Example 2

root = []return = []
root
empty

An empty tree has no deletion operations.

Example 3

root = [7]return = [7]
root
7

A leaf can be deleted immediately.

Constraints

  • 0 <= node count <= 200000.
  • -10^9 <= node.val <= 10^9.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Render a Comment HierarchyAmazon · ONSITE INTERVIEW
  • Kth Smallest Element in a Binary Search TreeSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Flatten and Reconstruct an Ordered TreeThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
  • Inorder, Preorder and Postorder TraversalsAMD · ONSITE INTERVIEWAMD interview questions
  • Compare N-Ary Tree Leaf SequencesNavan · ONSITE INTERVIEWNavan interview questions
  • Flatten JSON into Repeated Key-Value PairsKlaviyo · ONSITE INTERVIEWKlaviyo interview questions
  • Kth Smallest Value in a BSTExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
  • Validate Binary Search TreeInMobi · ONSITE INTERVIEWInMobi interview questions
  • Serialize a Binary Search TreeConfluent · ONSITE INTERVIEWConfluent interview questions
All Google interview questionsSee Google hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int[] deleteTreeOrder(TreeNode root) {
        // Write your code here.
    }
}
Checking account…