//use recursion
public static void mirrorHelp1(Node node){
if(node==null)return;
swapChild(node);
mirrorHelp1(node.getLeft());
mirrorHelp1(node.getRight());
}
//use no recursion but stack
public static void mirrorHelp2(Node node){
if(node==null)return;
Stack<Node> stack=new Stack<Node>();
stack.add(node);
while(!stack.isEmpty()){
node=stack.pop();
swapChild(node);
if(node.getLeft()!=null){
stack.push(node.getLeft());
}
if(node.getRight()!=null){
stack.push(node.getRight());
}
}
}
public static void swapChild(Node node){
/*not like c/c++,you cannot do this:
Node temp=left;
left=right;
right=temp;
*/
Node left=node.getLeft();
node.setLeft(node.getRight());
node.setRight(left);
}
java-15.输入一颗二元查找树,将该树转换为它的镜像, 即在转换后的二元查找树中,左子树的结点都大于右子树的结点。 用递归和循环...
最新推荐文章于 2017-09-28 14:06:07 发布