Untitled
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; }
Leave a Comment