LeetCode题解501.Find-Mode-in-Binary-Search-Tree

Problem on Leetcode

https://leetcode.com/problems/find-mode-in-binary-search-tree/

Description

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 node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

For example:
Given BST [1,null,2,2],

   1
    \
     2
    /
   2

return [2].

Note: If a tree has more than one mode, you can return them in any order.

Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).

Ideas

Basically, it needs traversing, counting and recording. map can be used to help us to do record and count.
For doing that without using any extra space, the property of BST will be used. For each node, the value of the left child is no greater than its value, while the value of right child is no less than the its value. So, when traversing each node, only the value of previous node is required to be compared with the value of current node for counting.
As the problem shown, an array of intergers will be returned. While the number of modes is unknown, using ArrayList to store all outputs is a good choice because ArrayList can be converted into array conveniently.

Codes

Supported Language: Java

  • Java Code:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {   
    List<Integer> list = new ArrayList<> ();
    TreeNode preNode = null;
    int max = 0, count = 0;
    
    public int[] findMode(TreeNode root) {
        helper(root);
        int[] res = new int[list.size()];
        for (int i=0; i<res.length; i++) {
            res[i] = list.get(i);
        }
        return res;
    }
    
    private void helper (TreeNode root) {
        if (root == null) return;
        helper(root.left);
        
        if (preNode != null && root.val == preNode.val) {
            count++;
        } else {
            count = 1;
        }
        
        if (count > max) {
            list.clear();
            list.add(root.val);
            max = count;
        } else if (max == count) {
            list.add(root.val);            
        }
        preNode = root;
        helper(root.right);
    }
}
LeetCode题解1261.find-elements-in-a-contaminated-binary-tree

题目地址(1261. 在受污染的二叉树中查找元素) https://leetcode-cn.com/problems/find-elements-in-a-contaminated-binary-tree/submissions/ 题目描述 给出一个满足下述规则的二叉树: root.val == 0 如果 treeNode.val == x 且 treeNo…

LeetCode题解1104.path-in-zigzag-labelled-binary-tree

题目地址 https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/description/ 题目描述 在一棵无限的二叉树上,每个节点都有两个子节点,树中的节点 逐行 依次按 “之” 字形进行标记。 如下图所示,在奇数行(即,第一行、第三行、第五行……)中,按从左到右的顺序进行标…

LeetCode题解124.binary-tree-maximum-path-sum

题目地址 https://leetcode.com/problems/binary-tree-maximum-path-sum/description/ 题目描述 Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as a…

LeetCode题解145.binary-tree-postorder-traversal

题目地址 https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 题目描述 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bi…

LeetCode题解144.binary-tree-preorder-traversal

题目地址 https://leetcode.com/problems/binary-tree-preorder-traversal/description/ 题目描述 Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null…