501. Find Mode in Binary Search Tree
2017-03-18 11:26
351 查看
Given a binary search tree (BST) withduplicates, find all the mode(s) (the most frequently occurred element) in thegiven BST.
Assume a BST is defined as follows:
The left subtree of a node contains onlynodes with keys less than or equal to the node's key.
The right subtree of a node contains onlynodes with keys greater than or equal to the node's key.
Both the left and right subtrees must alsobe 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, youcan return them in any order.
Follow up: Could you do that without usingany extra space? (Assume that the implicit stack space incurred due torecursion does not count).
翻译:给定具有重复的二叉搜索树(BST),找到给定BST中的所有模式(最频繁出现的元素)。
假设BST定义如下:节点的左子树仅包含键小于或等于节点键的节点。节点的右子树仅包含密钥大于或等于节点密钥的节点。左和右子树都必须是二叉搜索树。
例如:
给定BST [1,null,2,2],
1
\“
2
/
2
return [2]。
注意:如果树有多个模式,您可以按任何顺序返回它们。
跟进:你能没有使用任何额外的空间吗? (假设由于递归导致的隐式堆栈空间不计数)。
分析:求二叉搜索树的节点中那几个出现的次数最后,找到并返回一个整形数组。由于二叉搜索树的中序遍历时一个递增的数列,所有将它进行中序遍历。Max是节点中出现的次数最大值,num表示当前的数值,numValue表示当前数字出现的次数。代码如下:
/**
*Definition for a binary tree node.
*public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
List<Integer> ls=new LinkedList();
int max=0,num=Integer.MIN_VALUE,numValue=0;
public void orderVisit(TreeNode root){
if (root == null) return;
orderVisit(root.left);
// Visit(root);
numValue++;
if (root.val != num) {
num = root.val;
numValue = 1;
}
if (numValue > max) {
max = numValue;
ls.clear();;
ls.add(root.val);
} else if (numValue == max) {
ls.add(root.val);
}
orderVisit(root.right);
}
public int[] findMode(TreeNode root) {
//中序遍历后会产生一组数据
orderVisit(root);
int []sum=new int[ls.size()];
for(int i=0;i<ls.size();i++){
sum[i]=ls.get(i);
}
return sum;
}
}
Assume a BST is defined as follows:
The left subtree of a node contains onlynodes with keys less than or equal to the node's key.
The right subtree of a node contains onlynodes with keys greater than or equal to the node's key.
Both the left and right subtrees must alsobe 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, youcan return them in any order.
Follow up: Could you do that without usingany extra space? (Assume that the implicit stack space incurred due torecursion does not count).
翻译:给定具有重复的二叉搜索树(BST),找到给定BST中的所有模式(最频繁出现的元素)。
假设BST定义如下:节点的左子树仅包含键小于或等于节点键的节点。节点的右子树仅包含密钥大于或等于节点密钥的节点。左和右子树都必须是二叉搜索树。
例如:
给定BST [1,null,2,2],
1
\“
2
/
2
return [2]。
注意:如果树有多个模式,您可以按任何顺序返回它们。
跟进:你能没有使用任何额外的空间吗? (假设由于递归导致的隐式堆栈空间不计数)。
分析:求二叉搜索树的节点中那几个出现的次数最后,找到并返回一个整形数组。由于二叉搜索树的中序遍历时一个递增的数列,所有将它进行中序遍历。Max是节点中出现的次数最大值,num表示当前的数值,numValue表示当前数字出现的次数。代码如下:
/**
*Definition for a binary tree node.
*public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
List<Integer> ls=new LinkedList();
int max=0,num=Integer.MIN_VALUE,numValue=0;
public void orderVisit(TreeNode root){
if (root == null) return;
orderVisit(root.left);
// Visit(root);
numValue++;
if (root.val != num) {
num = root.val;
numValue = 1;
}
if (numValue > max) {
max = numValue;
ls.clear();;
ls.add(root.val);
} else if (numValue == max) {
ls.add(root.val);
}
orderVisit(root.right);
}
public int[] findMode(TreeNode root) {
//中序遍历后会产生一组数据
orderVisit(root);
int []sum=new int[ls.size()];
for(int i=0;i<ls.size();i++){
sum[i]=ls.get(i);
}
return sum;
}
}
相关文章推荐
- leetcode 501 Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- Leetcode 501 Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- LeetCode - 501 - Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree#2
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- leetcode-501-Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- Find Mode in Binary Search Tree
- [LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数
- LeetCode.501 Find Mode in Binary Search Tree