二叉树总结:入口
二叉树的基本操作:
5、判断一个二叉树是否是BST树,判断一个BST树是否是AVl树
7、把BST树满足[begin,end]区间的值放在集合中、打印出来
BST树的镜像
前序遍历二叉树,先将根节点入栈,然后交换其左右子树,再将子树入栈以此类推。
使用递归一样是先交换根节点左右子树,再进入孩子节点,以此类推。
//求BST的镜像 交换每一颗子树的左右孩子
public void mirror(BSTNode root){
if(root == null){
return;
}
Stack<BSTNode> stack = new Stack<BSTNode>();
stack.push(root);
while(!stack.isEmpty()){
BSTNode node = stack.pop();
if(node.getLeft() != null||node.getRight() != null){
BSTNode temp = node.getLeft();
node.setLeft(node.getRight());
node.setRight(temp);
}
if(node.getLeft()!=null){
stack.push(node.getLeft());
}
if(node.getRight()!=null){
stack.push(node.getRight());
}
}
}
// 递归求镜像
public void mirror1(BSTNode root){
if(root==null){
return;
}
BSTNode temp =root.getLeft();
root.setLeft(root.getRight());
root.setRight(temp);
mirror1(root.getLeft());
mirror1(root.getRight());
}