这次是二叉搜索树的遍历

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

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

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

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

本题显然是需要全局参数

/**
* 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. FPGA 串口

    VerilogHDL那些事儿_建模篇(黑金FPGA开发板配套教程) 作者:akuei2 说明:参照该书将部分程序验证学习一遍 学习时间:2014年5月3号 主要收获: 1. 对串口有初步了解: 2. ...

  2. 第一次UML作业

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2/ 这个作业要求在哪里 https://edu.cnblogs.com/campus/f ...

  3. C语言位运算符详解

    原文链接:https://www.cnblogs.com/911/archive/2008/05/20/1203477.html 位运算是指按二进制进行的运算.在系统软件中,常常需要处理二进制位的问题 ...

  4. NOIP2020退役记

    Day 0 这一次因为疫情仍然没有试机,但是允许提前打开虚拟机,减少了调试虚拟机的时间 开考时,吸取上一次 \(CSP\) 的教训,先把题面看了一遍 感觉 \(T1\) 比较可做,剩下的暂时没有思路 ...

  5. 第4.7节 Python特色的序列解包、链式赋值、链式比较

    一.序列解包 序列解包(或可迭代对象解包):解包就是从序列中取出其中的元素的过程,将一个序列(或任何可迭代对象)解包,并将得到的值存储到一系列变量中. 一般情况下要解包的序列包含的元素个数必须与你在等 ...

  6. Python(Python+Qt)学习随笔:使用xlwings新建Execl文件和sheet的方法

    在<Python学习随笔:使用xlwings读取和操作Execl文件>介绍了使用xlwings读取和操作Execl文件的方法,但老猿这两天写个例子使用时,发现使用该文的方法无法新建EXCE ...

  7. CentOS下安装SublimeText

    1.Install the GPG key: sudo rpm -v --import https://download.sublimetext.com/sublimehq-rpm-pub.gpg 2 ...

  8. john快速破解各种散列hash

    0x01 john工具安装: kali系统自带的有,也可以自己安装在其他linux和windows系统上,关于安装步骤网上都有可自行百度. 0x02 john常用选项 具体的其他选项可通过john - ...

  9. C#中SQL SERVER 2008字符数据类型使用心得

    一.尽可能使用Varchar,少使用或者不使用Char字符类型 因为char类型输入的数据长度达不到设计长度,会用空格补足,下面是数据表设计图: 下面是编辑前200行的图: 凡是输入的数据长度达不到设 ...

  10. Day4 Scrum 冲刺博客

    线上会议: 昨天已完成的工作与今天计划完成的工作及工作中遇到的困难: 成员姓名 昨天完成工作 今天计划完成的工作 工作中遇到的困难 纪昂学 创建一个Tetromino类来作为7个经典形状的父类 绘制下 ...