二叉树

BtNode类的构建

public class BinaryTree {
private BtNode cur;
private BtNode root;
	class BtNode // binaryTreeNode
	    {
	        int data;
	        BtNode leftchild;
	        BtNode rightchild;
	        public BtNode()
	        {
	            data = 0;
	            leftchild = rightchild = null;
	        }
	        public BtNode(int x)
	        {
	            data = x;
	            leftchild = rightchild = null;
	        }
	        public BtNode(int x,BtNode left,BtNode right)
	        {
	            data = x;
	            leftchild = left;
	            rightchild = right;
	        }
	    }

先序和中序创建二叉树

public void CreateTreePI(int []pre,int []in)
  {
      if(pre == null || in == null ||
              pre.length < 1 || in.length <1
              || pre.length != in.length)
      {
          return ;
      }
      int n = pre.length;
      root = CreatePI(pre,0,n-1,in,0,n-1);
  }
private BtNode CreateIP(int []in,int is,int ie,int []pa,int ps,int pe)
    {
        BtNode s = null;
        if(ie - is >= 0)
        {
            s = new BtNode(pa[pe]);
            int pos = FindIs(in,is,ie,pa[pe]);
            if(pos == -1) return null;
            int dist = pos - is;
            s.leftchild = CreateIP(in,is,is+dist-1,pa,ps,ps+dist-1);
            s.rightchild = CreateIP(in,is+dist+1,ie,pa,ps+dist,pe-1);
        }
        return s;
    }
private int FindIs(int[] in,int begin,int end,int val)
{
    int pos = -1;
    for(int i = begin; i<=end;++i)
    {
        if(in[i] == val)
        {
            pos = i;
            break;
        }
    }
    return pos;
}

剑指Offer上的 写法

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.Arrays;
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        if (pre.length == 0 || in.length == 0) {
            return null;
        }
        TreeNode root = new TreeNode(pre[0]);
        // 在中序中找到前序的根
        for (int i = 0; i < in.length; i++) {
            if (in[i] == pre[0]) {
                // 左子树,注意 copyOfRange 函数,左闭右开
                root.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i + 1), Arrays.copyOfRange(in, 0, i));
                // 右子树,注意 copyOfRange 函数,左闭右开
                root.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i + 1, pre.length), Arrays.copyOfRange(in, i + 1, in.length));
                break;
            }
        }
        return root;
    }
}

中序和后序创建二叉树

 public void CreateTreeIP(int []in,int []pa)
    {
        if(pa == null || in == null ||
                pa.length < 1 || in.length <1
                || pa.length != in.length)
        {
            return ;
        }
        int n = pa.length;
        root = CreateIP(in,0,n-1,pa,0,n-1);
    }
    private BtNode CreateIP(int []in,int is,int ie,int []pa,int ps,int pe)
    {
        BtNode s = null;
        if(ie - is >= 0)
        {
            s = new BtNode(pa[pe]);
            int pos = FindIs(in,is,ie,pa[pe]);
            if(pos == -1) return null;
            int dist = pos - is;
            s.leftchild = CreateIP(in,is,is+dist-1,pa,ps,ps+dist-1);
            s.rightchild = CreateIP(in,is+dist+1,ie,pa,ps+dist,pe-1);
        }
        return s;
    }
     private int FindIs(int[] in,int begin,int end,int val)
    {
        int pos = -1;
        for(int i = begin; i<=end;++i)
        {
            if(in[i] == val)
            {
                pos = i;
                break;
            }
        }
        return pos;
    }

非递归的先序遍历

public void NicePreOrder()
    {
        if(root == null)return ;
        Stack<BtNode> st = new Stack<>();
        st.push(root);
        while(!st.empty())
        {
            BtNode ptr = st.peek(); st.pop();
            System.out.print(ptr.data + " ");
            if(ptr.rightchild != null)
            {
                st.push(ptr.rightchild);
            }
            if(ptr.leftchild != null)
            {
                st.push(ptr.leftchild);
            }
        }
        System.out.println();
    }

非递归中序遍历

void NiceInOrder()
    {
        if(root == null) return ;
        BtNode ptr = root;
        Stack<BtNode> st = new Stack<>();
        while(ptr != null || !st.empty())
        {
            while (ptr != null) {
                st.push(ptr);
                ptr = ptr.leftchild;
            }
            ptr = st.peek();
            st.pop();
            System.out.print(ptr.data + " ");
            ptr = ptr.rightchild;
        }
        System.out.println();
    }

非递归的后序遍历

判断树的子结构

输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

public class Solution {
    //遍历大树
    public boolean HasSubtree(TreeNode root1,TreeNode root2) {
        if(root1 == null || root2 == null){
            return false;
        }
        //如果找到与子树相同根的值,走判断方法
        if(root1.val == root2.val){
            if(judge(root1,root2)){
                return true;
            }
        }
        //遍历左孩子,右孩子
        return HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);
    }
     
    //判断是否是子结构
    public boolean judge(TreeNode root, TreeNode subtree) {
        //子结构已经循环完毕,代表全部匹配
        if(subtree == null){
            return true;
        }
        //大树已经循环完毕,并未成功匹配
        if(root == null){
            return false;
        }
        //相等后判断左右孩子
        if(root.val == subtree.val){
            return judge(root.left, subtree.left) && judge(root.right, subtree.right);
        }
        return false;
    }
}

二叉树序列化和反序列化

链接: link.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值