Untitled
unknown
plain_text
10 months ago
589 B
3
Indexable
TreeNode mirrorTree(TreeNode root) {
if (root == null) return null;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode current = queue.poll();
// Swap the left and right children
TreeNode temp = current.left;
current.left = current.right;
current.right = temp;
// Add the children to the queue if they exist
if (current.left != null) queue.add(current.left);
if (current.right != null) queue.add(current.right);
}
return root;
}
Editor is loading...
Leave a Comment