树 java

public class TreeNode {
    int value;
    TreeNode leftChild;
    TreeNode rightChild;
    TreeNode(int value){
        this.value = value;
    }
    TreeNode(){

    }

    public void addLeft(int value){
        TreeNode leftChild=new TreeNode(value);
        this.leftChild=leftChild;
    }

    public void addRight(int value){
        TreeNode rightChild=new TreeNode(value);
        this.rightChild=rightChild;
    }
    
}
 /***
     * 树的节点总数
     * @param root
     * @return
     */
    public static int getTreeNum(TreeNode root){
        if(root ==null){
            return 0;
        }
        return getTreeNum(root.leftChild)+getTreeNum(root.rightChild)+1;
    }

    /**
     * 树的深度
     * @param root
     * @return
     */
    public static int getTreeDepth(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftDepth=getTreeDepth(root.leftChild)+1;
        int rightDepth=getTreeDepth(root.rightChild)+1;
        return Math.max(leftDepth,rightDepth);
    }


    /***
     * 前序遍历
     * @param root
     */
    public static void preOrderTravel(TreeNode root){
        if(root==null){
            return;
        }
        visitNode(root);
        preOrderTravel(root.leftChild);
        preOrderTravel(root.rightChild);
    }

    /***
     * 中序遍历
     * @param root
     */
    public static void midOrderTravel(TreeNode root){
        if(root==null){
            return ;
        }
        midOrderTravel(root.leftChild);
        visitNode(root);
        midOrderTravel(root.rightChild);
    }

    /***
     * 后序遍历
     * @param root
     */
    public static void backOrderTravel(TreeNode root){
        if(root==null){
            return;
        }
        backOrderTravel(root.leftChild);
        backOrderTravel(root.rightChild);
        visitNode(root);
    }

    /***
     * 访问node节点
     * @param node
     */
    public static void visitNode(TreeNode node){
        System.out.println(node.value+"\t");
    }

    /***
     * 分层遍历
     * @param root
     */
    public static void levelTravel(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        if (!queue.isEmpty()) {
            TreeNode temp = queue.poll();
            visitNode(temp);
            if(temp.leftChild!=null){
                queue.offer(temp.leftChild);
            }
            if(temp.rightChild!=null){
                queue.offer(temp.rightChild);
            }
        }
    }

    /***
     * 第k层节点个数
     * @param root
     * @param k
     * @return
     */
    public static int getNumForKlevel(TreeNode root,int k){
        if(root==null || k<1){
            return 0;
        }
        if(k==1){
            return 1;
        }
        int leftNum=getNumForKlevel(root.leftChild,k-1);
        int rightNum=getNumForKlevel(root.rightChild,k-1);
        int allNum=leftNum+rightNum;
        return allNum;
    }

    /***
     * 获取叶子节点个数
     * @param root
     * @return
     */
    public static int getLeafNum(TreeNode root){
        if(root ==null){
            return 0;
        }
        if(root.leftChild==null && root.rightChild==null){
            return 1;
        }
        int leftNum=getLeafNum(root.leftChild);
        int rightNum=getLeafNum(root.rightChild);
        int leafAllNum=leftNum+rightNum;
        return leafAllNum;
    }

    /***
     * 交换根节点的左右子树
     * @param root
     * @return
     */
    public static TreeNode exchangeRootChild(TreeNode root){
        if(root==null){
            return null;
        }
        TreeNode left=exchangeRootChild(root.leftChild);
        TreeNode right=exchangeRootChild(root.rightChild);
        root.leftChild=right;
        root.rightChild=left;
        return root;
    }

    public static boolean nodeIsChild(TreeNode root,TreeNode node){
        if(root==null || node==null){
            return false;
        }
        if(root==node){
            return true;
        }
        boolean isFind=nodeIsChild(root.leftChild,node);
        if(!isFind){
             isFind=nodeIsChild(root.rightChild,node);
        }
        return isFind;
    }

 

posted @ 2020-07-21 16:19  等木鱼的猫  阅读(115)  评论(0编辑  收藏  举报