This question evaluates proficiency in mutable data structure operations, specifically linked list node removal and binary tree node replacement/deletion, requiring knowledge of traversal, pointer/reference manipulation, and edge-case handling.
You are asked to solve two short coding tasks. You may assume standard node definitions:
val
,
next
val
,
left
,
right
Given the head of a singly linked list and an integer n, remove the n-th node from the end of the list and return the new head.
Requirements
1 <= n <= length(list)
O(L)
time where
L
is the list length, and
O(1)
extra space.
Given the root of a binary tree (not necessarily a BST) and an integer key, delete one node whose value equals key and return the (possibly new) root.
To keep the tree structure valid after deletion, perform deletion using this rule:
X
with value
key
.
D
.
X.val
with
D.val
.
D
from its parent by setting the corresponding child pointer to
null
.
Edge cases
key
is not found, return the original root.
key
, return
null
.
Complexity target: O(N) time, O(N) space is acceptable (e.g., BFS queue), where N is the number of nodes.