[LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数
这次是二叉搜索树的遍历
感觉只要和二叉搜索树的题目,都要用到一个重要性质:
中序遍历二叉搜索树的结果是一个递增序列;
而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回上层,参数也需要最新的),就要用全局变量,而不是传参,其实这就是全局变量的定义。
不过如果是保存每层递归内的信息,就需要传参数。
本题显然是需要全局参数
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
/*
思路还是利用二叉搜索树的重要性质:
中序遍历的结果是一个递增序列
比较当前元素和上个元素的数判断是不是同一个数,记录每个数出现的次数
根据次数,不断更新最后的结果
*/
List<Integer> res = new ArrayList<>();
int pre = Integer.MIN_VALUE;
int cur = 0;
int max = 0;
public int[] findMode(TreeNode root) {
if (root==null) return new int[0];
inOrder(root);
int[] a = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
a[i] = res.get(i);
}
return a;
}
public void inOrder(TreeNode root)
{
if (root==null) return;
System.out.println(pre);
inOrder(root.left);
if (root.val==pre)
{
cur++;
}
else
cur=1;
pre = root.val;
if (cur>max)
{
max = cur;
res.clear();
res.add(root.val);
}
else if (cur==max)
{
res.add(root.val);
}
inOrder(root.right);
}
}
[LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数的更多相关文章
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- 235 Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
给定一棵二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-s ...
- 35. leetcode 501. Find Mode in Binary Search Tree
501. Find Mode in Binary Search Tree Given a binary search tree (BST) with duplicates, find all the ...
- LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...
- [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...
随机推荐
- VM15 Ubuntu18.04 安装c/c++
输入"su"再输入密码即进入根用户,(gcc是默认安装的,但刚安装完成的系统中的gcc并不能用来开发,还缺少常用的头文件和库文件,还组要安装build-essential 软件包) ...
- WireShark抓包分析以及对TCP/IP三次握手与四次挥手的分析
WireShark抓包分析TCP/IP三次握手与四次挥手 Wireshark介绍: Wireshark(前称Ethereal)是一个网络封包分析软件.功能十分强大,是一个可以在多个操作系统平台上的开源 ...
- docker镜像无法删除 Error:No such image:xxxxxx
前言 docker镜像无法删除,通过 docker images 查看镜像明明存在就是删除不了. 删除提示:Error:No such image:xxxxxxx 具体截图内容如下: 解决方法 进入目 ...
- 腾讯短信平台ASP接口范例
疫情后一个小项目要用到腾讯短信平台,因为比较老,用ASP写的,平台没有相应的ASP接口,百度不到,无奈之下自己写了一个,也方便需要的朋友们. 主要代码如下: <!--#include file= ...
- JAVA课堂题目--递归来判断回数
package class20190923; import java.util.Scanner; public class Classtext { private static int n=0; pr ...
- [BJDCTF 2nd]duangShell 反弹shell
[BJDCTF 2nd]duangShell [BJDCTF 2nd]duangShell 点击进去之后提示我们swp源代码泄露,访问http://xxx/.index.php.swp下载该文件 ...
- Github 美化设置个人主页
起因是发现自己follow的大师傅个人主页跟普通的不太一样: 猜测应该是Github啥时候出现的新功能,查了一下,发现可以通过创建同名仓库来实现个人主页的美化设置 首先在 GitHub 上建立一个与自 ...
- PHP代码审计分段讲解(11)
后面的题目相对于之前的题目难度稍微提升了一些,所以对每道题进行单独的分析 27题 <?php if(!$_GET['id']) { header('Location: index.php?id= ...
- flask实现分类搜索的小测试
最新学长要求实现一个搜索的功能呢,也费了一点功夫.这个案例也没有学长写的好,比学长的实现差了不少,待我仔细研究习再发出相应代码 项目要求,搜索语法如下: titile: xxx #搜索titile的所 ...
- TMOOC-1709-小明复仇
题目描述 小明所在的世界上一共有n个城市,城市间有m条双向道路.小明现在在城市1,他想到位于城市n的小韩隆家询问他为什么没有将自己的五三复原完成.由于小韩隆手下有许多小弟,小明担心自己可能再也回不来, ...