← Oracle Interview Insights

Oracle·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Oracle SWE interview with a tree problem that looks straightforward but has a subtle recursive twist. Not the worst session I've had but the edge case caught me mid-explanation.

Questions Asked (1)

Q1

Given a binary tree and a target value, remove all leaf nodes equal to that target. If removing those leaves causes a parent node to become a new leaf with the same value, remove that too. Keep going until no such leaves remain. Return the modified tree.

Algorithms & Data Structures
Author's notes

I coded a postorder traversal pretty fast and felt good about it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order DFS to recursively process children before the current node, so that deletions propagate upward. At each node, after processing its subtrees, check if it has become a leaf with the target value and return null if so, otherwise return the node. This naturally handles cascading removals in a single pass.

Pro tip: Emphasize that the solution is O(n) time and O(h) space, and discuss how the post-order traversal ensures that a node is only evaluated after its children are finalized, which is key to handling cascading deletions correctly.

1. Clarify the problem and edge cases

Confirm that the tree may be empty, that the target can be any integer, and that the root itself might be removed. Discuss whether the tree is binary (not BST) and if node values can repeat.

2. Choose the traversal strategy

Select a post-order DFS because it processes children before the parent, allowing you to determine if a node becomes a leaf after its children are removed.

3. Define the recursive function

Write a function that takes a node and returns the node after processing. Recursively process left and right children, then check if the current node is a leaf with the target value; if so, return null, else return the node.

4. Handle the root and return the result

Call the recursive function on the root and return its result, which may be null if the entire tree is removed.

5. Analyze complexity and test

State that time complexity is O(n) and space is O(h) due to recursion stack. Walk through a small example to verify cascading deletions.

Key Points to Mention

  • Post-order traversal ensures children are processed before parents, enabling cascading deletions.
  • The base case for recursion: if node is null, return null.
  • After recursive calls, check if node.left and node.right are both null and node.val equals target.
  • Return null to remove the node, otherwise return the node itself.
  • Time complexity O(n) and space complexity O(h) where h is tree height.
  • Edge cases: empty tree, root removal, and target not present.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.