提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
别忘了这是搜索树,遍历整棵搜索树简直是对搜索树的侮辱。搜索树是有方向了,可以根据插入元素的数值,决定递归方向
一、力扣501. 二叉搜索树中的众数
/**
* 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) {
fun(root);
Deque<Map.Entry<Integer,Integer>> deq = new LinkedList<>();
for(Map.Entry<Integer,Integer> entry: map.entrySet()){
Integer key = entry.getKey();
Integer value = entry.getValue();
while(!deq.isEmpty()){
Map.Entry<Integer,Integer> p = deq.peekFirst();
if(value > p.getValue()){
deq.pollFirst();
}else{
break;
}
}
if(deq.isEmpty() || (!deq.isEmpty() && value == deq.peekFirst().getValue())){
deq.offerFirst(entry);
}
}
int[] res = new int[deq.size()];
for(int i = 0; i < res.length; i ++){
res[i] = deq.pollFirst().getKey();
}
return res;
}
public void fun(TreeNode root){
if(root == null){
return ;
}
fun(root.left);
map.put(root.val,map.getOrDefault(root.val,0)+1);
fun(root.right);
}
}
二、力扣236. 二叉树的最近公共祖先
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q){
return root;
}
TreeNode l = lowestCommonAncestor(root.left,p,q);
TreeNode r = lowestCommonAncestor(root.right,p,q);
if(l == null && r == null){
return null;
}else if(l != null && r == null){
return l;
}else if(l == null && r != null){
return r;
}else{
return root;
}
}
}
三、力扣235. 二叉搜索树的最近公共祖先
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null){
return null;
}
if(root.val > p.val && root.val > q.val){
return lowestCommonAncestor(root.left,p,q);
}else if(root.val < p.val && root.val < q.val){
return lowestCommonAncestor(root.right,p,q);
}else{
return root;
}
}
}
四、力扣701. 二叉搜索树中的插入操作
在这里插入代码片/**
* 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 TreeNode insertIntoBST(TreeNode root, int val) {
if(root == null){
return new TreeNode(val);
}
if(root.val > val){
root.left = insertIntoBST(root.left,val);
}else if(root.val < val){
root.right = insertIntoBST(root.right,val);
}
return root;
}
}
该博客围绕LeetCode上的二叉树与二叉搜索树题目展开,包含501题求二叉搜索树众数、236题找二叉树最近公共祖先、235题找二叉搜索树最近公共祖先以及701题在二叉搜索树中插入操作等内容,涉及算法与数据结构知识。
1万+

被折叠的 条评论
为什么被折叠?



