对树节点的定义
public class TreeNode {
protected int val ;
protected TreeNode left;
protected TreeNode right;
public TreeNode(int val) {
this.val = val;
this.left = null;
this.right = null;
}
public TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
对树的先序遍历
public void preTraback(TreeNode root) {
/**
* 对树进行先序遍历
*/
if(root != null) {
System.out.print(root.val + " ");
}
if (root.left == null) {
return;
}else {
preTraback(root.left);
}
if(root.right == null) {
return;
}else {
preTraback(root.right);
}
}
获取对应二叉树的镜像
public void Mirror(TreeNode root) {
if(root == null)
return;
if(root.left == null && root.right == null)
return;
TreeNode pTemp = root.left;
root.left = root.right;
root.right = pTemp;
if(root.left != null)
Mirror(root.left);
if(root.right != null)
Mirror(root.right);
}