题目链接:https://leetcode.cn/problems/find-mode-in-binary-search-tree/
方法一 暴力-二叉树
1 方法思想
2 代码实现
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
Map<Integer, Integer> map = new HashMap<>();
public int[] findMode(TreeNode root) {
noteNode(root);
int[][] result = new int[map.size()][2];
int index = 0;
for (Map.Entry<Integer, Integer> integerIntegerEntry : map.entrySet()) {
result[index][0] = integerIntegerEntry.getKey();
result[index++][1] = integerIntegerEntry.getValue();
}
Arrays.sort(result, (a, b) -> b[1] - a[1]);
int firt = result[0][1];
index = 1;
for (int i = 1; i < result.length; i++) {
if (result[i][1] != firt) break;
index++;
}
int[] ans = new int[index];
for (int i = 0; i < index; i++) {
ans[i] = result[i][0];
}
return ans;
}
public void noteNode(TreeNode root) {
if (root == null) return;
map.put(root.val, map.getOrDefault(root.val, 0) + 1);
noteNode(root.left);
noteNode(root.right);
}
}
3 复杂度分析
时间复杂度:
空间复杂度:
4 涉及到知识点
5 总结
方法二 迭代-二叉树
1 方法思想
2 代码实现
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int[] findMode(TreeNode root) {
if (root == null) return null;
TreeNode pre = null;
List<Integer> result = new ArrayList<>();
int count = 0;
int max = Integer.MIN_VALUE;
Stack<TreeNode> stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
if (pre == null || root.val != pre.val) {
count = 1;
} else {
count++;
}
if (count > max) {
result.clear();
result.add(root.val);
max = count;
} else if (count == max) {
result.add(root.val);
}
pre = root;
root = root.right;
}
int[] ans = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
ans[i] = result.get(i);
}
return ans;
}
}
3 复杂度分析
时间复杂度:
空间复杂度:
4 涉及到知识点
5 总结
方法三 递归-二叉搜索树
1 方法思想
2 代码实现
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
TreeNode pre = null;
List<Integer> result = new ArrayList<>();
int count = Integer.MIN_VALUE;
int max = Integer.MIN_VALUE;
public int[] findMode(TreeNode root) {
if (root == null) return null;
find(root);
int[] ans = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
ans[i] = result.get(i);
}
return ans;
}
public void find(TreeNode root) {
if (root == null) return;
find(root.left);
if (pre == null || root.val != pre.val) {
count = 1;
} else {
count++;
}
if (count > max) {
result.clear();
max = count;
result.add(root.val);
} else if (count == max) {
result.add(root.val);
}
pre = root;
find(root.right);
}
}
3 复杂度分析
时间复杂度:
空间复杂度: