FunctionRacerProblemsRace

Delete a Node from a BST

MediumTreesPython, JavaScript, TypeScript, Java, C++

You are given the root of a binary tree. Delete the node with value key (if there is none, change nothing) and return the root. Use exactly this rule:

Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.

Example 1

root [5,3,6,2,4,null,7]
key 3
returns [5,4,6,2,null,null,7]

3 has two children; the smallest in its right subtree is 4, so 3 becomes 4 and the old 4 is removed: [5,4,6,2,None,None,7].

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Trees problems