这次是二叉搜索树的遍历

感觉只要和二叉搜索树的题目,都要用到一个重要性质:

中序遍历二叉搜索树的结果是一个递增序列;

而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回上层,参数也需要最新的),就要用全局变量,而不是传参,其实这就是全局变量的定义。

不过如果是保存每层递归内的信息,就需要传参数。

本题显然是需要全局参数

/**
* 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二叉搜索树寻找众数的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  5. 235 Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先

    给定一棵二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-s ...

  6. 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  ...

  7. 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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Cloud-Native! 实战 Helm 3 部署 Traefik 2

    介绍 Traefik 是什么? Traefik, The Cloud Native Edge Router Traefik 是一种现代 HTTP 反向代理和负载均衡器,用于轻松部署微服务. 这篇文章对 ...

  2. Java数据结构(十三)—— 二叉排序树(BST)

    二叉排序树(BST) 需求 给定数列{7,3,10,12,5,1,9},要求能够高效的完成对数据的查询和添加 思路三则 使用数组,缺点:插入和排序速度较慢 链式存储,添加较快,但查找速度慢 使用二叉排 ...

  3. 20190705_关于winform程序修改程序名后, 报未将对象引用设置到对象的实例

    winform做了一个小项目, 其中要用到数据库连接, 字符串, private string ConnStringSource = System.Configuration.Configuratio ...

  4. activiti笔记

    activiti笔记 核心api api介绍 1.ProcessEngine 说明: 1) 在Activiti中最核心的类,其他的类都是由他而来. 2) 产生方式: ProcessEngine pro ...

  5. springmvc<二> 一些配置

    1.1.3. Web MVC Config    1.1.2中的解析器可以自定义实现,DispatcherServlet检查每个特殊bean的WebApplicationContext,如果没有匹配的 ...

  6. Python字符串学习相关问题

    Python中format_map与format字符串格式化的区别 Python中使用f字符串进行字符串格式化的方法 Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同? ...

  7. Hive基本原理及配置Mysql作为Hive的默认数据库

    Hive是什么? Hive是基于Hadoop之上的数据仓库: Hive是一种可以存储.查询.分析存储在hadoop中的大规模数据 Hive定义了简单的类SQL查询语言,成为HQL,它允许熟悉SQL的用 ...

  8. MacOS上Parallels Desktop安装MacOSHighSierra

    下载dmg文件 http://www.macoshome.com/macos/977.html 配置宿主机时间2015-10-27 23:25 为了防止"macOS未能安装在您的电脑上_安装 ...

  9. 博客中css样式的正确设置

    一.简介 博客园的文章是支持html代码和css样式的,即使是markdown写作.当某个标签需要特制样式时,我们可以自定义样式来覆盖掉原本的样式. 二.css样式优先级 参考至>>菜鸟教 ...

  10. 基于.NET的程序读取Excel文件的解决方案

    目录 0. 前言 1. 使用NPOI库读取Excel文件 2. 使用OleDbConnection 3. 相关参考 shanzm-2020年12月8日 23:48:11 0. 前言 以前基于 .NET ...