• javase(3)_二叉树


    // 1.求二叉树中的节点个数
    // 2.求二叉树的深度
    // 3.前序遍历,中序遍历,后序遍历
    // 4.分层遍历二叉树(按层次从上往下,从左往右)
    // 5.将二叉查找树变为有序的双向链表
    // 6.求二叉树第K层的节点个数
    // 7.求二叉树中叶子节点的个数
    // 8.判断两棵二叉树是否结构相同
    // 9.判断二叉树是不是平衡二叉树
    // 10.判断二叉树是不是完全二叉树

    节点

    class Node{
        Node leftChild;
        Node rightChild;
        Object value;
        public Node(Node leftChild, Node rightChild, Object value) {
            this.leftChild = leftChild;
            this.rightChild = rightChild;
            this.value = value;
        }
    }

    实现

    //求二叉树节点个数
    public static int getNodeNum(Node root){
        if(root==null)
            return 0;
        return getNodeNum(root.leftChild)+getNodeNum(root.rightChild)+1;
    }
    //求二叉树的深度
    public static int getDepth(Node root){
        if(root==null)
            return 0;
        int leftDepth = getDepth(root.leftChild);
        int rightDepth = getDepth(root.rightChild);
        return leftDepth>=rightDepth?leftDepth+1:rightDepth+1;
    }
    //前序遍历,中续遍历,后续遍历
    public static void preOrderTraversal(Node root){
        if(root==null)
            return;
        System.out.print(root.value);
        preOrderTraversal(root.leftChild);
        preOrderTraversal(root.rightChild);
    }
    public static void inOrderTraversal(Node root){
        if(root==null)
            return;
        inOrderTraversal(root.leftChild);
        System.out.print(root.value);
        inOrderTraversal(root.rightChild);
    }
    public static void postOrderTraversal(Node root){
        if(root==null)
            return;
        postOrderTraversal(root.leftChild);
        postOrderTraversal(root.rightChild);
        System.out.print(root.value);
    }
    //分层遍历二叉树(按层次从上往下,从左往右)
    public static void levelTraversal(Node root){
        if(root==null)
            return;
        LinkedList<Node> ll = new LinkedList<Node>();
        ll.offer(root);
        while(!ll.isEmpty()){
            root=ll.poll();
            System.out.println(root.value);
            if(root.leftChild!=null)
                ll.offer(root.leftChild);
            if(root.rightChild!=null)
                ll.offer(root.rightChild);
        }
    }
    //二叉查找树变为有序的双向链表
    static LinkedList<Node> ll=new LinkedList<Node>();
    public static LinkedList<Node>  binarySearchTreetoDoubleEndList(Node node){
        if(node==null)
            return null;
        binarySearchTreetoDoubleEndList(node.leftChild);
        ll.offer(node);
        binarySearchTreetoDoubleEndList(node.rightChild);
        return ll;
    }
    //求二叉树第K层的节点个数
    public static int getNodeNumkthLevel(Node root,int k){
        if(root==null)
            return 0;
        if(root.leftChild==null&&root.rightChild==null) //if(k==1)
            return 1;
        int leftNum=getNodeNumkthLevel(root.leftChild,k-1);
        int rightNum=getNodeNumkthLevel(root.rightChild,k-1);
        return leftNum+rightNum;
    }
    //求二叉树中叶子节点的个数
    public static int getleafNodeNum(Node root){
        if(root==null)
            return 0;
        if(root.leftChild==null&&root.rightChild==null)
            return 1;
        int leftNum=getleafNodeNum(root.leftChild);
        int rightNum=getleafNodeNum(root.rightChild);
        return leftNum+rightNum;
    }
    //判断两棵二叉树是否结构相同
    public static boolean isSameTree(Node n1,Node n2){
        if(n1==null&&n2==null)
            return true;
        if(n1==null||n2==null)
            return false;
        boolean l = isSameTree(n1.leftChild,n2.leftChild);
        boolean r = isSameTree(n1.rightChild,n2.rightChild);
        return l&&r;
    }
    //判断二叉树是不是平衡二叉树
    public static boolean isBalanceTree(Node root){
        if(Math.abs(getDepth(root.leftChild)-getDepth(root.rightChild))>1)
            return false;
        if(Math.abs(getDepth(root.leftChild)-getDepth(root.rightChild))<1)
            return true;
        boolean l = isBalanceTree(root.leftChild);
        boolean r = isBalanceTree(root.rightChild);
        return l&&r;
    }
    //判断二叉树是不是完全二叉树
    public static boolean isCompleteTree(Node root){
        if(root.leftChild==null&&root.rightChild==null)
            return true;
        if(root.leftChild==null||root.rightChild==null)
            return false;
        boolean l = isCompleteTree(root.leftChild);
        boolean r = isCompleteTree(root.rightChild);
        return l&&r;
    }

      

  • 相关阅读:
    最少乘法次数 http://acm.nyist.net/JudgeOnline/problem.php?pid=46
    取石子(一)http://acm.nyist.net/JudgeOnline/problem.php?pid=23
    数的长度http://acm.nyist.net/JudgeOnline/problem.php?pid=69
    Dinner http://acm.nyist.net/JudgeOnline/problem.php?pid=218
    FatMouse' Trade http://acm.hdu.edu.cn/showproblem.php?pid=1009
    Elevator http://acm.hdu.edu.cn/showproblem.php?pid=1008
    Number Sequence http://acm.hdu.edu.cn/showproblem.php?pid=1005
    阶乘之和http://acm.nyist.net/JudgeOnline/problem.php?pid=91
    对象模型学习总结 (二) . 关于继承和多态
    cubieboard 配置WIFI
  • 原文地址:https://www.cnblogs.com/wangweiNB/p/4805346.html
Copyright © 2020-2023  润新知