[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 ...
随机推荐
- spring boot 访问外部http请求
以前 访问外部请求都要经过 要用 httpClient 需要专门写一个方法 来发送http请求 这个这里就不说了 网上一搜全都是现成的方法 springboot 实现外部http请求 是通过F ...
- 20190627_解决ADB的device offline问题的两种方法
故障现象: error: device offline 故障解决: 第一种方法: C:\Users\WXY\Desktop\XY\adb>adb nodaemon server cannot b ...
- 第2.2节 Python的语句
上节已经介绍了极简的Python代码编写,已经用到了赋值语句,本节对Python的程序语句进行介绍. 一. 常用命令 在介绍Python语句之前,先介绍一下几个有用的Python命令. dir(模块名 ...
- 第15.11节 PyQt(Python+Qt)入门学习:Qt Designer(设计师)组件Property Editor(属性编辑)界面中主窗口QMainWindow类相关属性详解
概述 主窗口对象是在新建窗口对象时,选择main window类型的模板时创建的窗口对象,如图: 在属性编辑界面中,主窗口对象与QMainWindow相关的属性包括:iconSize.toolButt ...
- Monkey的使用
1.进入adb shell 环境 在Windows环境下进入DOS界面,输入adb shell 注意:adb shell服务使用的端口是5037,如果此端口被其他进程占用时,将不能正常启动adb sh ...
- NET core 添加了新的nuget包,部署出现Could not load file or assembly
这个坑,今天整了一天,我添加了Microsoft.AspNetCore.Mvc.Versioning包,结果发布到服务器,我复制了dll过去出现了一直找不到加载不成功的问题 Startup.Confi ...
- System.out.println();快捷键
不同开发工具的输出快捷键是不一样的-System.out.println(); eclipse&my eclipse中是syso +(alt+/) idea 是sout +tab 或者sout ...
- 使用Promise实现红绿灯交替重复亮
红灯3秒亮一次,黄灯2秒亮一次,绿灯1秒亮一次:如何让三个灯不断交替重复亮灯?(用Promise实现) function red() { console.log('red'); } function ...
- 代理ip知识
一.没有使用代理服务器的情况: REMOTE_ADDR = 您的 IP HTTP_VIA = 没数值或不显示 HTTP_X_FORWARDED_FOR = 没数值或不显示 二.使用 ...
- python爬虫--用xpath爬豆瓣电影
步骤 将目标网站下的页面抓取下来 将抓取下来的数据根据一定规则进行提取 具体流程 将目标网站下的页面抓取下来 1. 倒库 import requests 2.头信息(有时候可不写) headers ...