Give a binary tree, and its mirror image, the following figure: the right is a two-fork tree is the mirror of the left binary tree.
Solution 1 (Recursive)
Idea 1: If the current node is empty, return, otherwise exchange the left and right nodes of the node, recursively exchange processing on its left node.
/*class treenode{
int val;
TreeNode Left=null;
TreeNode Right=null;
Public TreeNode (int val) {
this.val = val;
}
} */public
static void Mirrortree (TreeNode root)
{
if (root==null) return
;
Swap the left and right nodes that the node points to.
TreeNode Temp=root.left;
Root.left=root.right;
Root.right=temp;
Mirror the left and right children.
Mirrortree (root.left);
Mirrortree (root.right);
}
The exchange process is shown below:
Idea 2: If the current node is NULL, return NULL, otherwise the left and right children of the node will be mirrored, then point the node to the right-hand child, the right pointer to the left child, the node mirroring.
/*class treenode{
int val;
TreeNode Left=null;
TreeNode Right=null;
Public TreeNode (int val) {
this.val = val;
}
} *
/public static TreeNode mirrorTree1 (TreeNode root)
{
if (root==null) return
null;
To the left and right children mirror processing
TreeNode left=mirrortree1 (root.left);
TreeNode right=mirrortree1 (root.right);
Mirrors the current node.
Root.left=right;
Root.right=left;
return root;
}
Solution 2 (not recursive)
Idea 1: Hierarchical traversal, the root node is not NULL will be the root node, the team is not empty, the node out of the team, exchange the node of the left and right children, if the left and right children are not empty, will be left and right children.
public static void Mirrortreewithqueue (TreeNode root) {if (root==null) return; If the tree returns directly to NULL.
Otherwise, the root node is queued.
queue<treenode> queue= new linkedlist<treenode> ();
Queue.add (root);
while (!queue.isempty ()) {//queue is not empty, the node is out of the team, swapping the left and right subtree of the node.
TreeNode Root1=queue.poll ();
/*treenode Left,right;
Left=root1.left;
Right=root1.right;
Root1.right=left;
Root1.left=right;
*/Swap (root);
if (root1.right!=null) {queue.add (root1.right);
If the Zoozi tree is not NULL join} if (Root1.left!=null) {queue.add (root1.left);
If the right subtree is not NULL to join the team.
}} public static void Swap (TreeNode root) {TreeNode temp;
Temp=root.right;
Root.right=root.left;
Root.left=temp; }
Idea 2: first-order traversal, if the root node is not null to the root node into the stack, when the stack is not null out of the stack, swap the left and right nodes, if the left and right nodes are not null into the stack.
public static void Mirrortreewithstack (TreeNode root) {if (root==null)
Return
Stack<treenode> stack=new stack<treenode> ();
Stack.push (root);
while (!stack.isempty ()) {//stack when stack is not NULL, swap left and right subtree.
TreeNode Root1=stack.pop ();
/*treenode Left,right;
Left=root1.left;
Right=root1.right;
Root1.right=left;
root1.left=right;*/Swap (root);
if (root1.right!=null) {//right subtree is not null into stack Stack.push (root1.right);
} if (Root1.left!=null) {//Zoozi tree is not null into stack Stack.push (root1.left);
}} public static void Swap (TreeNode root) {TreeNode temp;
Temp=root.right;
Root.right=root.left;
Root.left=temp; }