LeetCode 501. 二叉搜索树中的众数

截止到目前我已经写了 500多道算法题,其中部分已经整理成了pdf文档,目前总共有1000多页(并且还会不断的增加),大家可以免费下载
下载链接https://pan.baidu.com/s/1hjwK0ZeRxYGB8lIkbKuQgQ
提取码:6666

在这里插入图片描述


二叉树的中序遍历解决

在这里插入图片描述

public void inOrderTraversal(TreeNode node) {
    if (node == null)
        return;
    inOrderTraversal(node.left);
    System.out.println(node.val);
    inOrderTraversal(node.right);
}

我们来对他进行改造一下

List<Integer> mList = new ArrayList<>();
int curent = 0;//表示当前节点的值
int count = 0;//和当前节点值相同的节点数量
int maxCount = 0;//最大的重复数量

public int[] findMode(TreeNode root) {
    inOrderTraversal(root);
    int[] res = new int[mList.size()];
    //把集合list转化为数组
    for (int i = 0; i < mList.size(); i++) {
        res[i] = mList.get(i);
    }
    return res;
}

//递归方式
public void inOrderTraversal(TreeNode node) {
    //终止条件判断
    if (node == null)
        return;
    //遍历左子树
    inOrderTraversal(node.left);

    //下面是对当前节点的一些逻辑操作
    int nodeValue = node.val;
    if (nodeValue == curent) {
        //如果节点值等于curent,count就加1
        count++;
    } else {
        //否则,就表示遇到了一个新的值,curent和count都要
        //重新赋值
        curent = nodeValue;
        count = 1;
    }
    if (count == maxCount) {
        //如果count == maxCount,就把当前节点加入到集合中
        mList.add(nodeValue);
    } else if (count > maxCount) {
        //否则,当前节点的值重复量是最多的,直接把list清空,然后
        //把当前节点的值加入到集合中
        mList.clear();
        mList.add(nodeValue);
        maxCount = count;
    }

    //遍历右子树
    inOrderTraversal(node.right);
}

之前在讲到373,数据结构-6,树的时候,提到二叉树中序遍历的非递归写法

public void inOrderTraversal(TreeNode tree) {
    Stack<TreeNode> stack = new Stack<>();
    while (tree != null || !stack.isEmpty()) {
        while (tree != null) {
            stack.push(tree);
            tree = tree.left;
        }
        if (!stack.isEmpty()) {
            tree = stack.pop();
            System.out.println(tree.val);
            tree = tree.right;
        }
    }
}

再来改造一下

List<Integer> mList = new ArrayList<>();
int curent = 0;
int count = 0;
int maxCount = 0;

public int[] findMode(TreeNode root) {
    inOrderTraversal(root);
    int[] res = new int[mList.size()];
    //把集合list转化为数组
    for (int i = 0; i < mList.size(); i++) {
        res[i] = mList.get(i);
    }
    return res;
}

//非递归方式
public void inOrderTraversal(TreeNode tree) {
    Stack<TreeNode> stack = new Stack<>();
    while (tree != null || !stack.isEmpty()) {
        while (tree != null) {
            stack.push(tree);
            tree = tree.left;
        }
        if (!stack.isEmpty()) {
            tree = stack.pop();
            int nodeValue = tree.val;
            if (nodeValue == curent) {
                //如果节点值等于curent,count就加1
                count++;
            } else {
                //否则,就表示遇到了一个新的值,curent和count都要
                //重新赋值
                curent = nodeValue;
                count = 1;
            }
            if (count == maxCount) {
                //如果count == maxCount,就把当前节点加入到集合中
                mList.add(nodeValue);
            } else if (count > maxCount) {
                //否则,当前节点的值重复量是最多的,直接把list清空,然后
                //把当前节点的值加入到集合中
                mList.clear();
                mList.add(nodeValue);
                maxCount = count;
            }
            tree = tree.right;
        }
    }
}

总结

做这题首先要搞懂二叉搜索树的中序遍历结果是排序的,这题就容易解了。除了常规的二叉树的中序遍历,我们还可以使用二叉树的Morris中序遍历,具体可以看下《488,二叉树的Morris中序和前序遍历》

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据结构和算法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值