这次是二叉搜索树的遍历

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

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

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

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

本题显然是需要全局参数

/**
* 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. oracle 流程控制句式

    --for loop declare val number(10):=0; begin for val in 0..10 loop dbms_output.put_line('val='||val); ...

  2. 再见!【JAVA GUI】

    1.GUI概述 图形用户界面(Graphical User Interface,简称 GUI,又 称图形用户接口)是指采用图形方式显示的计算机操作用户界面. java作为一种面向对象的程序设计语言,它 ...

  3. 树莓派搭建seafile服务器备忘

    用户:pi 密码:raspberry 启用root用户https://blog.csdn.net/chenxd1101/article/details/53437925(防止特殊原因pi用户不能登录时 ...

  4. 2017 Mid Central Regional G.Hopscotch (组合计数)

    这道题有点意思,给出点(N,N),你在原点处向目标点走,每次只能向x和y两个方向走路,每次xy两个方向的步幅分别不能小于dx和dy,问走到终点的方案数,答案对1e9 + 7取模 这道题最直接的想法就是 ...

  5. 第15.21节 PyQt(Python+Qt)入门学习:QListView的作用及属性详解

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 QListView是从QAbstractItemView 派生的类,实现了QAbstrac ...

  6. 问题:PyCharm调试方法smart step into的用途

    smart step into为智能单步跟踪,当一行代码中有多个函数,想进入其中一个函数调测其他函数不进入调测时,使用该功能可以让调试人员选择进入的函数.如: 就可以选择需要调试进入的函数而其他两个函 ...

  7. DVWA SQL Injection High

    High 虽然是high等级,但是通过源码审计发现与low等级一样,没有对传入的值做任何过滤,唯一不同的就是点击连接后打开了另外一个对话框,用户在新打开的页面输入 其余的步骤与low级别的一样:htt ...

  8. git 常用命令 command

    git config --list //查看配置信息   git config user.name //查看用户名  git config user.email //查看用户邮箱 从远程克隆到本地仓库 ...

  9. 【科技】位运算(bitset)优化最长公共子序列算法

    最长公共子序列(LCS)问题 你有两个字符串 \(A,B\),字符集为 \(\Sigma\),求 \(A, B\) 的最长公共子序列. 简单动态规划 首先有一个广为人知的 dp:\(f_{i,j}\) ...

  10. day106:MoFang:BUG:获取数据验证token是否过期&相册/相机取消头像无法显示&MongoDB

    目录 BUG1:前端在获取数据时,要检验token是否过期 BUG2:相册/相机取消后设置页面头像无法显示 MongoDB 1.MongoDB基本介绍 2.MongoDB安装 3.MongoDB:通用 ...