二叉树算法整理

二叉树算法整理

二叉树构建

private class Tree
{
    int val;
    Tree left;
    Tree right;
    private  Tree(int val)
{
    this.val=val;

}
       int lm;
     int rm;
}
  static   int  [] array={1,2,3,4,5,6,7,8,9};
  static   List<Tree> newnode=null;
   private void Createtree(int[] array)
{
    //先把数组中的数据都变成节点
    newnode=new LinkedList<>();
    for(int i=0;i<array.length;i++)
    {
        Tree newtree=new Tree(array[i]);
        newnode.add(newtree);
    }
    //把节点连起来
    for(int k=0;k<array.length/2-1;k++)
    {
        //左树
        newnode.get(k).left=newnode.get(k*2+1);
        newnode.get(k).right=newnode.get(k*2+2);

    }
    //如果是偶数 就没有最右边的孩子
    int j=array.length/2-1;
    newnode.get(j).left=newnode.get(j*2+1);
    if(array.length%2!=0)
        newnode.get(j).right=newnode.get(j*2+2);
}

先序遍历

private static void xianxu(Tree tree)
    {
        if(tree==null)
            return;
        System.out.println(tree.val);
        xianxu(tree.left);
        xianxu(tree.right);
    }

分层打印二叉树,并且每层打一行

private static void fenceng(Tree tree)
    {
        if(tree==null)
            return;
        Deque<Tree> newnode=new LinkedList<>();
        newnode.add(tree);
        Tree last=tree;
        Tree nlast=tree;
        while (!newnode.isEmpty())
        {
            Tree pop=newnode.poll();

            System.out.print(pop.val);
            if(pop.left!=null) {
                newnode.add(pop.left);
                nlast=newnode.getLast();
            }
            if(pop.right!=null) {
                newnode.add(pop.right);
                nlast=newnode.getLast();
            }
            if(last==pop)
            {
                System.out.print("\n");
                last=nlast;
            }

        }

    }

二叉树深度(递归)

 private static int depth(Tree tree)
    {
        if(tree==null)
            return 0;
        int left=depth(tree.left);
        int right=depth(tree.right);
        int bigger =Math.max(left,right);
        return  bigger+1;
    }

二叉树两个节点的最短路径

static  int max=-1;
   //返回从开始节点能走到的最大距离,不是这个二叉树最左和最右节点的距离
    private  static  int maxDist(Tree tree)
    {
        if(tree==null)
            return  0;

        if(tree.left!=null)
            tree. lm=maxDist(tree.left)+1;

        if(tree.right!=null)
        tree.    rm=maxDist(tree.right)+1;

        int sum=tree.lm+tree.rm;

        //如果以该节点为根的子树中有最大距离,更新最大距离
        if(sum>max)
            max=sum;
     // if(tree.left==null&&tree.right==null)
         // return  max;
        return tree.lm>tree.rm?tree.lm:tree.rm;

    }

二叉树最短路径

 public  static int mindist(Tree root) {
        if(root == null)
            return 0;

        if(root == null) return 0;
        if(root.left == null && root.right != null){
            return mindist(root.right) + 1;
        }
        if(root.left != null && root.right == null){
            return mindist(root.left) + 1;
        }
        return Math.min(mindist(root.left),mindist(root.right))+1;


    }
 

之字形打印二叉树

//把奇偶行分别放在两个栈中,然后分别对他们进行循环
    public static   ArrayList<ArrayList<Integer>> zigzagLevelOrder(Tree root) {
        if(root==null)return  null;
       ArrayList <ArrayList<Integer>> finallist=new ArrayList<ArrayList<Integer>>() {};
                  Stack<Tree> stackji=new Stack<>();
        Stack<Tree> stackou=new Stack<>();
        stackji.push(root);
                   Tree  root1=root;
                   Tree root2=root;
                 int k=1;

               while (!stackji.isEmpty()||!stackou.isEmpty())
               {
                   Tree pop=null;

                   if(k%2!=0) {
                       ArrayList<Integer> list=new ArrayList<>();
                       while (!stackji.isEmpty()) {
                           pop = stackji.pop();
                           list.add(pop.val);
                           if (pop.right != null)
                               stackou.add(pop.right);
                           if (pop.left != null)
                               stackou.add(pop.left);
                       }
                       if(!list.isEmpty())
                       {
                           finallist.add(list);
                           k++;
                       }
                   }
                   else {
                       ArrayList<Integer> list=new ArrayList<>();
                       while (!stackou.isEmpty()) {
                           pop = stackou.pop();
                           list.add(pop.val);
                           if (pop.left != null)
                               stackji.add(pop.left);
                           if (pop.right != null)
                               stackji.add(pop.right);

                       }
                       if(!list.isEmpty())
                       {
                           finallist.add(list);
                           k++;
                       }
                   }


                   }
                 return  finallist;
                }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值