1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public void Mirror(TreeNode root) { if (root == null) { return; } Stack<TreeNode> stack = new Stack<>(); stack.push(root); while (!stack.empty()) { TreeNode tmp = stack.pop(); TreeNode left = tmp.left; tmp.left = tmp.right; tmp.right = left; if (tmp.left != null) { stack.push(tmp.left); } if (tmp.right != null) { stack.push(tmp.right); } } }
|