编辑代码

import java.util.LinkedList;
import java.util.Queue;
class Main {
	public static void main(String[] args) {
        //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
		System.out.println("Hello world!   - java.jsrun.net ");

        BinaryTree tree = new BinaryTree();
        TreeNode root = tree.newTree();
        tree.prevOrder(root);
        System.out.println(root.value);
	}
}
class TreeNode<T>
{
        public T value;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(T value)
        {
            this.value=value;
        }
}
class BinaryTree<T>
{

    public TreeNode newTree()
    {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        TreeNode I = new TreeNode("I");
        TreeNode J = new TreeNode("J");
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        D.left = I;
        D.right = J;
        E.right = H;
        return A;
    }
    // 前序遍历,次序 根,左,右
    public void prevOrder(TreeNode root)
    {
        if(root==null)
        {
            return;
        }
        System.out.print(root.value+" ");
        prevOrder(root.left);
        prevOrder(root.right);
    }
    // 中序遍历 次序 左,根,右
    public void inOrder(TreeNode root)
    {
        if(root==null)
        {
            return;
        }
        inOrder(root.left);
        System.out.print(root.value+" ");
        inOrder(root.right);
    }
    // 后序遍历,次序,左 右 根
    public void postOrder(TreeNode root)
    {
        if(root==null)
        {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.value+" ");
    }
    // 结点个数
    public int size(TreeNode root)
    {
        if(root==null)
        {
            return 0;
        }
        return 1+size(root.left)+size(root.right);
    }
    /*
    二叉树的叶子结点个数
    思想
        核心思想:
        1.左树的叶子结点+右树的叶子结点
        2.一个结点的左右结点都为空的时候,返回1
    */
    public int levelSize(TreeNode root)
    {
        if(root==null)
        {
            return 0;
        }

        if(root.left==null&&root.right==null)
        {
            return 1;
        }
        return levelSize(root.left)+levelSize(root.right);
    }
    /*
    6.二叉树的第K层结点个数
    6.1 思想
    核心思想
    1.左树第K层结点个数+右树第K层结点个数
    2.k=1时返回1
    */
    public int getKLevelNodeCount(TreeNode root,int k)
    {
        if(root==null)
        {
            return 0;
        }
        if(k==1)
        {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }
    // 获取二叉树的高度
    public int getHeight(TreeNode root)
    {
        if(root==null)
        {
            return 0;
        }
        int leftH=getHeight(root.left);
        int rightH=getHeight(root.right);

        return (leftH>=rightH?leftH:rightH)+1;
    }
    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, T val)
    {
        if(root==null)
        {
            return null;
        }
        if(root.value==val)
        {
            return root;
        }
        TreeNode leftNode=find(root.left,val);
        if(leftNode!=null)
        {
            return leftNode;
        }
        TreeNode rightNode=find(root.right,val);
        if(rightNode!=null)
        {
            return rightNode;
        }
        return null;
    }
    //层序遍历
    public void levelOrder(TreeNode root)
    {
        Queue<TreeNode> queue=new LinkedList<>();
        if(root!=null)
        {
            queue.offer(root);
        }
        while(!queue.isEmpty())
        {
            TreeNode cur=queue.poll();
            System.out.print(cur.value+" ");
            if(cur.left!=null)
            {
                queue.offer(cur.left);
            }
            if(cur.right!=null)
            {
                queue.offer(cur.right);
            }
        }
    }
}