[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);
    }
}

原文地址:https://www.cnblogs.com/stAr-1/p/8329583.html

时间: 2024-08-02 11:31:58

[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 BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的LCA

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

1099 Build A Binary Search Tree [二叉搜索树/中序、层次遍历]

先用中序确定节点的值,再用层次遍历输出即可. 写的时候思维江化,一开始用指针建树... #include <bits/stdc++.h> using namespace std; #define maxn 105 struct Node { int index,left,right; }node[maxn]; int n,a[maxn],tree[maxn]; vector<int> ve[maxn],in,post; void inorder(int p) { if(node[p

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 element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

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 element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

501. Find Mode in Binary Search Tree查找BST中的众数

[抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to t

132.Find Mode in Binary Search Tree(二分搜索树的众数)

题目: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. 给定具有重复项的二叉搜索树(BST),找到给定BST中的所有众数(最频繁出现的元素). Assume a BST is defined as follows: 假设BST定义如下: The left subtree of a node

LeetCode 501. Find Mode in Binary Search Tree(寻找二叉查找树中出现次数最多的值)

题意:寻找二叉查找树中出现次数最多的值 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int ma = 0; int cnt = 1; TreeNode

[leetcode]Convert Sorted List to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意:将一条排序好的链表转换为二叉查找树,二叉查找树需要平衡. 解题思路:两个思路:一,可以使用快慢指针来找到中间的那个节点,然后将这个节点作为树根,并分别递归这个节点左右两边的链表产生左右子树,这样的好处是不需要使用额外的空间,坏处是代码不够整洁.二,将排序好的链表的每个节点的值存入一个数组中,这样就和http://www.cnblog