二叉树day10

98. 验证二叉搜索树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
//中序递归
class Solution {
    TreeNode pre = null;
    public boolean isValidBST(TreeNode root) {
        return inOrder(root);
    }
    private boolean inOrder(TreeNode root) {
        if (root == null) return true;
        boolean left = inOrder(root.left);
        if (pre != null && pre.val >= root.val) return false;
        pre = root;
        boolean right = inOrder(root.right);
        return left && right;
    }
}

//中序迭代 得到是一个有序序列 且无重复元素
// class Solution {
    // public boolean isValidBST(TreeNode root) {
    //     TreeNode pre = null;
    //     Deque<TreeNode> stack = new LinkedList<>();
    //     stack.push(root);
    //     while (!stack.isEmpty()) {
    //         TreeNode cur = stack.peek();
    //         while (cur != null) {
    //             stack.push(cur.left);
    //             cur = cur.left;
    //         }
    //         //空指针退栈
    //         stack.pop();
    //         if (!stack.isEmpty()) {
    //             cur = stack.pop();
    //             if (pre != null && pre.val >= cur.val) return false;
    //             pre = cur;
    //             stack.push(cur.right);
    //         }
    //     }
    //     return true;
    // }
// }

530. 二叉搜索树的最小绝对差

//中序递归 中序遍历搜索二叉树的结点是一个递增序列
class Solution {
    TreeNode pre = null;
    int res = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        if (root.left != null) res = getMinimumDifference(root.left);

        if (pre != null) res = Math.min(root.val - pre.val, res);
        pre = root;

        if (root.right != null) res = getMinimumDifference(root.right);

        return res;
    }
}

501. 二叉搜索树中的众数

//使用额外空间 遍历树用HashMap存出现次数
//不用额外空间 中序遍历搜索树得到的是一个有序序列 直接统计连续值即可
class Solution {
    private TreeNode pre = null;
    private List<Integer> resList;
    private int count = 0;
    private int maxCount = 0;
    public int[] findMode(TreeNode root) {
        resList = new ArrayList<>();
        inOrder(root);
        int size = resList.size();
        int[] res = new int[size];
        for (int i = 0; i < size; i++) {
            res[i] = resList.get(i);
        }
        return res;
    }
    private void inOrder(TreeNode root) {
        if (root == null) return;
        if (root.left != null) inOrder(root.left);

        if (pre == null) count++;
        else if (root.val == pre.val) count++;
        //重新开始计数
        else count = 1;
        pre = root;
        if (count > maxCount) {
            maxCount = count;
            resList.clear();
            resList.add(root.val);
        } else if(count == maxCount) {
            resList.add(root.val);
        }

        if (root.right != null) inOrder(root.right);
    }
}

 

参考:programmercarl.com

posted @ 2022-04-06 20:06  一梦两三年13  阅读(16)  评论(0)    收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示