二叉树的非递归遍历、求高、节点数等方法

/**
     * 前序
     * @param root
     */
    public static void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode temp = stack.pop();
            if (temp == null) {
                continue;
            }
            System.out.println(temp.val);
            if (temp.right != null) {
                stack.push(temp.right);
            }
            if (temp.left != null) {
                stack.push(temp.left);
            }
        }
    }

    /**
     * 中序
     * @param root
     */
    public static  void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode temp = root;
        Stack<TreeNode> stack = new Stack<>();
        while (!stack.isEmpty() || temp != null) {
            while (temp != null) {
                stack.push(temp);
                temp = temp.left;
            }
            if (!stack.isEmpty()) {
                TreeNode t = stack.pop();
                System.out.println(t.val);
                temp = t.right;
            }
        }
    }

    /**
     * 后序
     * @param root
     */
    public static void reOrder (TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        LinkedList<Integer> list = new LinkedList<>();
        TreeNode node = root;
        stack.push(node);
        while (!stack.isEmpty()) {
            TreeNode temp = stack.pop();
            list.addFirst(temp.val);

            if (temp.left != null) {
                stack.push(temp.left);
            }
            if (temp.right != null) {
                stack.push(temp.right);
            }
        }

        System.out.println(list);
    }

    /**
     * 层次遍历
     * @param root
     */
    public static void floorOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        ArrayList<Integer> list = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int count = queue.size();
            while (count-- > 0) {
                TreeNode temp = queue.poll();
                if (temp == null) {
                    continue;
                }
                list.add(temp.val);
                queue.add(temp.left);
                queue.add(temp.right);
            }
        }
        System.out.println(list);
    }

    /**
     * 求树高
     * @param root
     * @return
     */
    public static int treeDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftDepth = treeDepth(root.left);
        int rightDepth = treeDepth(root.right);
        return 1 + Math.max(leftDepth, rightDepth);
    }

    /**
     * 判断是否是平衡二叉树
     * @param root
     * @return
     */
    public static boolean judgeAVL(TreeNode root) {
        if (root == null) {
            return true;
        }
        int left = treeDepth(root.left);
        int right = treeDepth(root.right);
        if (left - right > 1 || left - right < -1) {
            return false;
        }
        return judgeAVL(root.left) && judgeAVL(root.right);
    }

    /**
     * 二叉树的下一个节点,中序遍历的情况下
     * 额外有一个指向父节点的指针 next
     * @param root
     * @return
     */
    public static TreeNode getNext(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode node = null;
        //第一种情况:右子树不为空
        if (root.right != null) {
            node = root.right;
            while (node != null) {
                node = node.left;
            }
        }
        //第二种情况:右子树为空,且当前为父节点的左子节点
        if (root.right == null) {
            if (root.next != null) {
                if (root.next.left == root) {
                    node = root.next;

                    //第三种情况:右子树为空,且当前为父节点的右子节点
                }else if (root.next.right == root) {
                    while (root.next != null && root.next.right == root) {
                        root = root.next;
                    }
                    node = root.next;
                }
            }
        }
        return node;
    }

    /**
     * 镜像二叉树
     * @param root
     * @return
     */
    public static void mirron(TreeNode root) {
        if (root == null) {
            return;
        }
        //交换,可抽象出一个函数
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        mirron(root.left);
        mirron(root.right);
    }

    /**
     * 判断是不是对称二叉树
     * @param root
     * @return
     */
    public static boolean isBalance(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isBalance(root.left, root.right);
    }
    public static boolean isBalance(TreeNode tree1, TreeNode tree2) {
        if (tree1 == null && tree2 == null) {
            return true;
        }
        if (tree1 == null || tree2 == null) {
            return false;
        }
        if (tree1.val != tree2.val) {
            return false;
        }
        return isBalance(tree1.left, tree2.right) && isBalance(tree1.right, tree2.left);
    }

    /**
     * 二叉树中和为某一路径加起来的值
     */
    private static List<List<Integer>> res = new ArrayList<>();
    private static List<Integer> list = new ArrayList<>();
    public static List<List<Integer>> findTarget (TreeNode root, int target) {
        if (root == null) {
            return res;
        }
        findTarget2(root, target);
        return res;
    }
    public static void findTarget2 (TreeNode root, int target) {
        list.add(root.val);
        if (target == root.val && root.left == null && root.right == null) {
            res.add(new ArrayList<>(list));
        }
        if (root.left != null) {
            findTarget2(root.left, target - root.val);
        }
        if (root.right != null) {
            findTarget2(root.right, target - root.val);
        }
        list.remove(list.size() - 1);
    }

    /**
     * 二叉搜索树与双向链表
     */
    private static TreeNode pre = null;
    private static TreeNode head = null;
    public static TreeNode convert(TreeNode root) {
        trace(root);
        return head;
    }
    public static void trace(TreeNode root) {
        if (root == null) {
            return;
        }
        trace(root.left);
        if (pre != null) {
            pre.right = root;
            root.left = pre;
        }
        pre = root;
        if (head == null) {
            head = root;
        }
        trace(root.right);
    }

    /**
     * 二叉树的第k层节点个数
     * @param root
     * @param k
     * @return
     */
    public static int getCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        int left = getCount(root.left, k - 1);
        int right = getCount(root.right, k - 1);
        return left + right;
    }

    /**
     * 求二叉搜索树的任意两个节点值之差的最小值
     */
    private static int preMin = -1;
    private static int minNumber = Integer.MAX_VALUE;
    public static int getMin(TreeNode root) {
        inOrderGetMin(root);
        return minNumber;
    }
    public static void inOrderGetMin(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrderGetMin(root.left);
        if (preMin != -1) {
            minNumber = Math.min(minNumber, root.val - preMin);
        }
        preMin = root.val;
        inOrderGetMin(root.right);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值