二叉树常见算法题


/**
 * 树的常见算法
 * 1、递归,前中后序
 * 2、非递归前序
 * 3、非递归中续
 * 4、非递归后续
 * 5、层次遍历
 * 5、判断是否是平衡二叉树
 * 6、判断是否是搜索二叉树
 * 7、判断是否是完全二叉树
 * 8、返回最近公共祖先
 * 9、中继后续
 * 10、从前序与中序遍历序列构造二叉树 ...
 *
 */

public class TreeSummarize {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(5);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(7);

        preOrder(root);
        inOrder(root);
        postOrder(root);
        w(root);
        System.out.println(getMaxWidth(root));

        boolean balanced = isBalanced(root);
        System.out.println(balanced);

    }

    /**
     * 树的结构
     */
    static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    /**
     * 递归排序
     * @param root
     */
    public static void postOrder1(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder1(root.left);
        postOrder1(root.right);
        System.out.print(root.val + " ");
    }


    /**
     * 非递归前序
     * @param root
     */
    public static void preOrder(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        while (!stack.isEmpty()) {
            TreeNode pop = stack.pop();
            System.out.print(pop.val + " ");
            if (pop.right != null) {
                stack.add(pop.right);
            }
            if (pop.left != null) {
                stack.add(pop.left);
            }
        }
        System.out.println();
    }

    /**
     * 非递归中序
     * @param root
     */
    public static void inOrder(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        while (!stack.isEmpty() || root != null) {
            if (root != null) {
                stack.add(root);
                root = root.left;
            } else {
                root = stack.pop();
                System.out.print(root.val + " ");
                root = root.right;
            }
        }
        System.out.println();
    }

    /**
     * 非递归后序
     * @param root
     */
    public static void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> s1 = new Stack<>();
        Stack<TreeNode> s2 = new Stack<>();
        s1.add(root);
        while (!s1.isEmpty()) {
            root = s1.pop();
            s2.add(root);
            if (root.left != null) {
                s1.add(root.left);
            }
            if (root.right != null) {
                s1.add(root.right);
            }
        }
        while (!s2.isEmpty()) {
            System.out.print(s2.pop().val + " ");
        }
        System.out.println();
    }


    /**
     * 层次遍历
     * @param root
     */
    public static void w(TreeNode root) {
        if (root == null) {
            return;
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.add(cur.left);
            }
            if (cur.right != null) {
                queue.add(cur.right);
            }
        }
        System.out.println();
    }

    /**
     *
     * @param root
     * @return 求二叉树的最大宽度
     */
    public static int getMaxWidth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        // map 保存节点和层级,当层级发生改变,计算
        Map<TreeNode, Integer> map = new HashMap<>();

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        map.put(root, 1);
        int w = 0;
        int maxWidth = 0;
        int curH = 1;  // 当前层高
        int tempH = 1;  // 记录一层的层高,用来标记是否换层

        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            // 得到层高
            curH = map.get(node);
            if (curH != tempH) {
                tempH = curH;
                maxWidth = Math.max(maxWidth, w);
                w = 1;
            }else {
                w++;
            }
            if (node.left != null) {
                queue.add(node.left);
                map.put(node.left, curH + 1);
            }
            if (node.right != null) {
                queue.add(node.right);
                map.put(node.right, curH + 1);
            }
        }
        return maxWidth = Math.max(maxWidth, w);
    }


    /**
     * @param root
     * @return 平衡二叉树  https://leetcode.cn/problems/balanced-binary-tree/
     */
    public static boolean isBalanced(TreeNode root) {

        
    }


}

你可能感兴趣的:(leetcode,算法,java,数据结构)