树
public static TreeNode firCreateTree(TreeNode root,String str){
char ch = str.charAt(count++);
if(ch == '#'){
root = null;
return root;
}
root = new TreeNode();
root.val = ch;
root.left = firCreateTree(root.left,str);
root.right = firCreateTree(root.right,str);
return root;
}
- 遍历树(递归、非递归 (先序、中序、后序)):非递归运用了栈。
public static void firstSearch(TreeNode root){
if(root == null)return;
System.out.print(root.val+" ");
firstSearch(root.left);
firstSearch(root.right);
}
public static void firstSearch1(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
while(root != null || !stack.isEmpty()){
while(root != null){
stack.push(root);
System.out.print(root.val+" ");
root = root.left;
}
if(!stack.isEmpty()){
root = stack.pop();
root = root.right;
}
}
}
public static void midSearch(TreeNode root){
if(root == null)return;
midSearch(root.left);
System.out.print(root.val+" ");
midSearch(root.right);
}
public static void midSearch1(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
while(root != null || !stack.isEmpty()){
while(root != null){
stack.push(root);
root = root.left;
}
if(!stack.isEmpty()){
root = stack.pop();
System.out.print(root.val+" ");
root = root.right;
}
}
}
public static void lastSearch(TreeNode root){
if(root == null)return;
lastSearch(root.left);
lastSearch(root.right);
System.out.print(root.val+" ");
}
public static void lastSearch1(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
TreeNode temp = null;
while(root != null || !stack.isEmpty()){
while(root != null){
stack.push(root);
root = root.left;
}
if(!stack.isEmpty()){
root = stack.peek();
if(root.right == null || root.right == temp){
root = stack.pop();
System.out.print(root.val+" ");
temp = root;
root = null;
}else{
root = root.right;
}
}
}
}
public static void levelSearch(TreeNode root){
if(root == null)return;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
root = queue.poll();
System.out.print(root.val+" ");
if(root.left != null)queue.offer(root.left);
if(root.right != null)queue.offer(root.right);
}
}
- 求树的高度或深度:使用了成员变量height来进行更新,且传入的temp初始化是1。
public static void getHeight(TreeNode root,int temp){
if(root == null)return;
if(height < temp)height = temp;
getHeight(root.left,temp+1);
getHeight(root.right,temp+1);
}
- 求指定结点的双亲:有递归和非递归两种,非递归形式是在先序非递归的遍历基础上进行改造的,递归形式是在先序递归遍历的基础上改造的。end即为当前结点的双亲结点。
public static TreeNode getParent(TreeNode root,char ch){
TreeNode temp = root;
Stack<TreeNode> stack = new Stack<>();
while(root != null || !stack.isEmpty()){
while(root != null){
stack.push(root);
temp = root;
root = root.left;
if(root != null && root.val == ch && isParAndSon(temp,root)){
return temp;
}
}
if(!stack.isEmpty()){
root = stack.pop();
temp = root;
root = root.right;
if(root != null && root.val == ch && isParAndSon(temp,root)){
return temp;
}
}
}
return null;
}
public static void getParent1(TreeNode root,char ch,TreeNode end){
if(root == null)return;
if(root.val == ch){
if(end != null) System.out.println("该结点的双亲为:"+end.val);
else System.out.println("该结点的双亲为:"+end);
}
getParent1(root.left,ch,root);
getParent1(root.right,ch,root);
}
public static boolean isParAndSon(TreeNode parent,TreeNode son){
if( (parent.left != null && parent.left.val == son.val) || (parent.right != null && parent.right.val == son.val))return true;
return false;
}
public static boolean isSameTree(TreeNode tree1,TreeNode tree2){
if(tree1 == null && tree2 == null)return true;
if(tree1 == null || tree2 == null)return false;
return isSameTree(tree1.left,tree2.left)&&isSameTree(tree1.right,tree2.right);
}
- 根据先序中序创建二叉树:由先序确定根的位置,再在中序中分割左右子树。
public static TreeNode firAndMidCreate(char[] pre,char[] mid,int pres,int mids,int mide){
if( mide - mids <= 0){
return null;
}
TreeNode root = new TreeNode(pre[pres]);
System.out.println(pre[pres]);
int i;
for(i = mids;i < mide; i++){
if(pre[pres] == mid[i])break;
}
++pres;
root.left = firAndMidCreate(pre,mid,pres,mids,i);
root.right = firAndMidCreate(pre,mid,pres+i-mids,i+1,mide);
return root;
}
- 根据中序、后序创建二叉树:由后序确定根的位置,再在中序中分割左右子树。
public static TreeNode midAndLastCreate(char[] mid,char[] last,int lasts,int mids,int mide){
if((mide - mids) <= 0){
return null;
}
TreeNode root = new TreeNode(last[lasts+mide-mids-1]);
System.out.println(last[lasts+mide-mids-1]);
int i;
for(i = mids;i < mide; i++){
if(mid[i] == last[lasts+mide-mids-1])break;
}
root.left = midAndLastCreate(mid,last,lasts,mids,i);
root.right = midAndLastCreate(mid,last,lasts+i-mids,i+1,mide);
return root;
}