数据结构—二叉树

二叉树:
叶子/根/高度

链式表示法:利用树的节点表示二叉树,类似链表

class Node {
int val;
Node left;//左孩子,左子树的根
Node right;//右孩子,右子树的根

特殊二叉树:
完全二叉树/满二叉树
遍历方式:
1.深度优先遍历
前 中 后序

练习:
1.前中后序遍历

//前:根+左子树+右子树
//中:左子树+根+右子树
//后:左子树+右子树+根
public void preOrder(Node root){
if(root!=null){
System.out.println(root.val);
preOrder(root.left);
preOrder(root.right);
}
}
public void inOrder(Node root){
if(root!=null){
inOrder(root.left);
System.out.println(root.val);
inOrder(root.right);
}
}
public void postOrder(Node root){
if(root!=null){
postOrder(root.left);
postOrder(root.right);
System.out.println(root.val);
}
}

2.后序遍历,返回List

//汇总思想   
//左子树后序遍历的结果+右子树后序遍历的结果+根的值
public static List<Integer> postOrderTraveral(Node root){
List<Integer> result=new ArrayList<>();
if(root==null){
return result;
}
List<Integer> left=postOrderTraveral(root.left);
List<Integer> right=postOrderTraveral(root.right);
result.addAll(left);
result.addAll(right);
result.add(root.val);
return result;
}

3.判断两棵树是否相同

public boolean isSameTree(Node p,Node q){
if(p==null&&q==null){
return true;
}
if(p==null||q==null){
return false;
}
if((p.val==q.val)&&isSameTree(p.left,p.right)&&(isSameTree(p.right,q.right)){
return true;
}
reruen false;
}

4.另一个树的子树

//t是否是s的一棵子树,s和t一定不是空树
//在s这棵树中查找是否有和t相等的子树
public boolean isSubTree(Node s,Node t){
if(s==null){
return false;
}
//先看根所在的树是否和t相同
if(isSameTree(s,t)){
return tree;
}
//继续去左子树找t
if(isSubTree(s.left,t)){
return true;
}
//再去右子树找t
if(isSubTree(s.right,t)){
return true;
}
return false;
}

5.交换一次的先前排列(难)

public int[] prevPermOpt1(int[] A){
for(int i=A.length-2;i>=0;i--){
if(A[i]>A[i+1]{
int maxIndex=-1;
int max=Integer.MIN_VALUE;
for(int j=i+1;j<A.length;j++){
if(A[j]<A[i]&&A[j]>max){
max=A[j];
maxIndex=j;
//i是要交换的
//maxIndex就是要交换的位置
int t=A[i];
A[i]=A[maxIndex];
A[maxIndex]=t;
return A;
}
}
return A;
}

6.二叉树的最近公共祖先

private boolean search(Node r,Node t){
if(r==null){
return false;
}
if(r==t){
return true;
}
if(search(r.left,t)){
return true;
}
if(search(r.right,t)){
return true;
}
return false;
}

public Node lowestCommonAncestor(Node root,Node p,Node q){
if(p==root||q==root){
return root;
}
boolean pInLeft=search(root.left,p);
boolean qInLeft=search(root.left,q);
if(pInLeft&&qInLeft){
return lowestCommonAncestor(root.left,p,q);
}
if(!pInLeft&&!qInLeft){
return lowestCommonAncestor(root.right,p,q);
}
return root;
}

7.从前序与中序遍历序列构造二叉树

private static Node{
int val;
Node left;
Node right;
Node root(int x){
val=x;
}
}

public Node buildTree(int[] preorder,int[] inorder){
if(preorder.length==0){
return null;
}
int rootValue=preorder[0];
int leftCount;
for(leftCount=0;leftCount<inorder.length;leftCount++){
if(inorder[leftCount]==rootValue){
break;
}
Node root=new Node(rootValue);
}//左子树的结点个数为leftCount
 //左子树的前序+中序(做一个切分)
 //右子树的前序+中序
 int[] leftPreorder=Arrays.copyOfRange(preorder,1,1+leftCount);
 int[] leftInorder=Arrays.copyOfRange(inorder,0,leftCount);
 root.left=buildTree(leftPreorder,leftInorder);
 int[] rightPreorder=Array.copyOfRange(preorder,1+leftCount,preOrder.length);
 int[] rightInorder=Array.copyOfRange(inorder,1+leftCount,inOrder.length);
 root.right=buildTree(rightPreorder,rightInorder);
 return root;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值