二叉树常见算法题


/**
 * 树的常见算法
 * 1、递归,前中后序
 * 2、非递归前序
 * 3、非递归中续
 * 4、非递归后续
 * 5、层次遍历
 * 5、判断是否是平衡二叉树
 * 6、判断是否是搜索二叉树
 * 7、判断是否是完全二叉树
 * 8、返回最近公共祖先
 * 9、中继后续
 * 10、从前序与中序遍历序列构造二叉树 <a href="https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/">...</a>
 *
 */

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) {

        
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值