Clone Binary Tree - aloalgo

Clone Binary Tree

Easy

You are given the root of a binary tree, return a deep copy (clone) of the tree. A deep copy means creating new nodes for all nodes in the original tree and preserving the structure and values. The cloned tree should be completely independent of the original tree. You can assume the TreeNode structure is available with val, left, and right properties.

Example 1

Input
123
Output
123
Explanation:

The original tree has root 1, left child 2, and right child 3. The cloned tree will have the exact same structure and values, but all nodes will be new objects, completely independent.

Example 2

Input
123
Output
123
Explanation:

For a right-skewed tree (1 -> 2 -> 3), the clone maintains this structure with new nodes. The null values indicate missing children.

Example 3

Input
None
Output
None
Explanation:

Cloning an empty tree should result in an empty tree (represented as an empty array).

Loading...
Input
123
Output
123

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!