JAVA二叉树要比C++里好操作很多。
(1)A检测B是否是A的子树
/** 二叉树(A检测B是否是A的子树)
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
//子树检测函数
boolean flag = false;
boolean flag1 = false;
boolean flag2 = false;
if(root1==null||root2 == null) return false;
if(root1.val != root2.val){
flag1 = HasSubtree(root1.left,root2);
flag2 = HasSubtree(root1.right,root2);
}
else if(root1.val == root2.val){
//这里需要对元素相等的进行子树判断,并且继续递归子树检测
flag = compare(root1,root2)||HasSubtree(root1.left,root2)||HasSubtree(root1.right,root2);
}
return flag||flag1||flag2;
}
private boolean compare(TreeNode root1, TreeNode root2){
//子树判断函数
if(root1 == null&&root2 != null) return false;
else if(root1 != null&&root2 == null) return true;
//即使root1还不为空,但是root1也包含了root2
if(root1==null&&root2==null){
return true;
}
if(root1.val != root2.val) return false;
return compare(root1.left,root2.left)&&compare(root1.right,root2.right);
}
}
(2)输出二叉树的镜像
public void Mirror(TreeNode root) {
if(root == null) return;
TreeNode t = root.left;
root.left = root.right;
root.right = t;
Mirror(root.left);
Mirror(root.right);
}
(3)二叉树的后序遍历检测
public boolean VerifySquenceOfBST(int [] sequence) {
//非递归玩法
int size = sequence.length;
if(size == 0) return false;
int i=0;
while(size>0){
size--;
while(sequence[i++]<sequence[size]);
while(i<size&&sequence[i++]>sequence[size]);
if(i<size) return false;//如果i++最后没有到达size
i=0;//记得需要还原
}
return true;
}
后续遍历检测递归版本:
private static boolean SequenceBst(int [] sequence,int start,int end){
//递归玩法
/*int i=end-1;
while(sequence[i]>sequence[end]&&i>start) i--;
for(int j=start;j<i;j++){
if(sequence[j]>sequence[end])
return false;
}*/
/*
int i = start;
while(sequence[i]<sequence[end]&&i<end) i++;
for(int j=end-1;j>=i;j--) {
if(sequence[j]<sequence[end]) return false;
}
return SequenceBst(sequence,start,i-1)&&SequenceBst(sequence,i,end-1);
*/
if(sequence.length == 0 ) return false;//题目给定空为否
if(start >= end) return true; //大于等于是因为避免end-1之后小于start 标志着递归区间收缩到一个点
int t = start;
while(t<end&&sequence[t]<sequence[end]) {
t++;
}; //检索比根节点小的也就是左子树
boolean flag1 = SequenceBst(sequence,start,t-1);//递归左子树
int r = t; // t作为左子树和右子树的分界线
while(sequence[r]>sequence[end]&&r<end) {
r++;
};//检索比根节点大的也就是右子树
if(r<end) return false; // 检索完毕发现r不等于end说明该数组不是合法后序
boolean flag2 = SequenceBst(sequence,t,end-1);//递归检索右子树
return flag1 && flag2;
}
public static boolean Solution(int [] sequence) {
return SequenceBst(sequence,0,sequence.length-1);
}
(4)二叉树深度遍历&记录路径&检测路径长度是否等于目标值
输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
import java.util.*;
import java.lang.*;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
//重载compare方法
class MyIntComparator implements Comparator<ArrayList<Integer>>{
/**
* o1长度比o2长,那么返回1表示大于,小返回-1,等于返回0
*/
@Override
public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
// TODO Auto-generated method stub
if(o1.size()>o2.size()) return -1;
else if(o1.size()<o2.size()) return 1;
else return 0;
}
}
private ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
if(root==null) return result;
ArrayList<Integer> t = new ArrayList<Integer>() ;
t.add(root.val);
if(target-root.val==0) {
result.add(t);
}
DFS(root.right,target-root.val,t);
DFS(root.left,target-root.val,t);
//按照规则排序result数组
Collections.sort(result, new MyIntComparator());
return result;
}
//DFS部分 结束条件是遍历到空节点
private void DFS(TreeNode root,int target,ArrayList<Integer> t) {
if(root==null) return ;
t.add(root.val);
//如果左右结点为空,并且路径长度等于target目标值,将t存进result中
if(root.right==null&&root.left==null&&target-root.val==0) {
result.add(new ArrayList<Integer>(t));
//指向new对象,是因为对t直接赋值会导致,所有元素都指向同一个t。
}
DFS(root.right,target-root.val,t);
DFS(root.left,target-root.val,t);
t.remove(t.size()-1);//DFS遍历很重要的回滚!!!一定要谨记!
}
}