public static void mirror( Node root ) { Stack<Node> nodes = new Stack<Node>( ); nodes.push( root ); while( ! nodes.empty( ) ) { Node currentNode = nodes.pop( ); // Traverse the left subtree. if( currentNode.left != null ) { nodes.push( currentNode.left ); } // Traverse the right subtree. if( currentNode.right != null ) { nodes.push( currentNode.right ); } // Swap the left node with the right node. Node tempNode = currentNode.left; currentNode.left = currentNode.right; currentNode.right = tempNode; } }
Home » Binary Trees(We Are Computer Tree & NOT Free !) » Mirroring a Binary Tree