Binary Tree Deletion Order
Updated
Problem statement
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]Delete leaf 4 before its parent 2, then the right subtree rooted at 3, and finally root 1.
Example 2
root = []return = []An empty tree has no deletion operations.
Example 3
root = [7]return = [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