实现二叉树的基本操作

public class TreeNode {
    public char val;
    public TreeNode left;//左孩子的引用
    public TreeNode right;//右孩子的引用

    public TreeNode(char val) {
        this.val = val;
    }
}
1.创建一颗二叉树,返回这棵树的根节点
public class BinaryTree {
    public TreeNode createTree() {
        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 J = new TreeNode('J');
        TreeNode K = new TreeNode('K');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        D.left = J;
        D.right = K;
        return A;
    }

2.前序遍历

public void preOrder(TreeNode root) {
        //先左子树,差分为多个左右子树,中左右
        if (root == null) {
            return;
        }
        //先打印头结点,再依次打印左子树,右子树
        System.out.println(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

3.中序遍历

void inOrder(TreeNode root) {
        //左中右
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.println(root.val + " ");
        inOrder(root.right);
    }

4.后序遍历

 void postOrder(TreeNode root) {
        //左右中
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.val + " ");
    }

5.获取树中结点的个数:遍历思路

public static int nodeSize;

    /**
     * 获取树中节点的个数:遍历思路
     */
    void size(TreeNode root) {
        if (root == null) {
            return;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
    }

6.获取结点个数:子问题思路

int size2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return size2(root.right) + size2(root.left) + 1;
    }

7.获取叶子节点个数:遍历思路

public static int leafSize = 0;

    void getLeafNodeCount1(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

8.获取叶子节点个数:子问题思路

int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.right) +
                getLeafNodeCount2(root.left);
    }

9.获取第k曾结点的个数

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);
    }

10.获取二叉树的高度

int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return leftH > rightH ? leftH + 1 : rightH + 1;
    }

11.检测为value的元素是否存在

TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        //判断val在不在左子树
        TreeNode ret = find(root.left, val);
        if (ret != null) {
            return ret;
        }
        //判断val在不在右子树
        ret = find(root.right, val);
        if (ret != null) {
            return ret;
        }
        return null;
    }

12.层序遍历

扫描二维码关注公众号,回复: 16475740 查看本文章
void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        //定义一个队列,先进先出
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);//压入头结点
        while (!queue.isEmpty()) {//不为空
            TreeNode cur = queue.poll();
            System.out.println(cur.val + " ");
            //左子树不为空,压入左子树
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            //右子树不为空,压入右子树
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

13.判断一棵树是不是完全二叉树

boolean isCompleteTree(TreeNode root) {
        if (root == null) {
            return false;
        }
        //创建一个队列
        //完全二叉树,若左节点有左孩子没有右孩子,而右节点有孩子,则在判断出左节点没有左孩子的时候已经break停止循环,
        //在第二次遍历的时候右节点的孩子还储存在queue当中,说明不是完全二叉树
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);//压入头结点
        while (!queue.isEmpty()) {//不为空
            TreeNode cur = queue.poll();//让cur = 当前结点root
            if (cur != null) {//不为空压入左右结点
                queue.offer(cur.left);
                queue.offer(cur.right);
            } else {
                break;
            }
        }
        //判断队列中是否还存在非空元素,如果有,则不是完全二叉树
        TreeNode cur = queue.peek();
        if (cur == null) {
            queue.poll();
        } else {
            return false;
        }
        return true;
    }

猜你喜欢

转载自blog.csdn.net/crazy_tan/article/details/130851734
今日推荐