代码随想录算法训练营
今日任务
530.二叉搜索树的最小绝对差 ,501.二叉搜索树中的众数 ,236. 二叉树的最近公共祖先
530.二叉搜索树的最小绝对差
//迭代法
/**
* 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 getMinimumDifference(TreeNode root) {
int min=100005;
int lastrec=100005;
Stack<TreeNode> stack=new Stack<>();
stack.push(root);
while (!stack.isEmpty()){
TreeNode top = stack.peek();
if(top.left!=null){
stack.push(top.left);
top.left=null;
}else{
TreeNode pop = stack.pop();
int sub=Math.abs(pop.val-lastrec);
min=Math.min(min,sub);
if(top.right!=null){
stack.push(top.right);
top.right=null;
}
lastrec=pop.val;
}
}
return min;
}
}
//递归法
/**
* 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 {
int min=100005;
TreeNode pre;
public int getMinimumDifference(TreeNode root) {
if(root==null){
return 0;
}
inorder(root);
return min;
}
public void inorder(TreeNode root) {
if(root==null){
return;
}
inorder(root.left);
if(pre!=null){
min=Math.min(min,Math.abs(root.val-pre.val));
}
pre=root;
inorder(root.right);
}
}
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 {
int count=1;
int max=0;
TreeNode pre;
public int[] findMode(TreeNode root) {
List<Integer> list=new ArrayList<>();
inorder(root,list);
int size = list.size();
int[] res=new int[size];
for (int i = 0; i < res.length; i++) {
res[i]=list.get(i);
}
return res;
}
public void inorder(TreeNode root,List<Integer> list) {
if(root==null){
return;
}
//pre是上一个节点
inorder(root.left,list);
//System.out.println(root.val);
if(pre!=null){
if(pre.val==root.val){
count++;
}else{
count=1;
}
//说明root已经到了pre的后一个节点,count记录的是pre出现的次数
if(count>max){
//将原来list中存储的数据清空
list.clear();
list.add(pre.val);
max=count;
}else if(count==max){
list.add(root.val);
}
}else{
max= Math.max(max,count);
list.add(root.val);
}
pre=root;
inorder(root.right,list);
}
}
236. 二叉树的最近公共祖先
//自己写的八知道什么方法
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
TreeNode parent;
int pheight=-1;
int qheight=-1;
int ppos=-1;
int qpos=-1;
int height=0;
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
List<Integer> list=new ArrayList<>();
getDepth(root,p,q,list);
return parent;
}
public int getDepth(TreeNode root,TreeNode p,TreeNode q,List<Integer> list) {
if(root==null){
return height;
}
height++;
if(root.left!=null){
int left=getDepth(root.left,p,q,list);
if(left==-1){
return -1;
}
height=height-1;
}
if(root.right!=null){
int right=getDepth(root.right,p,q,list);
if(right==-1){
return -1;
}
height=height-1;
}
if(root.val==p.val){
pheight=height;
ppos=list.size();
}else if(root.val==q.val){
qheight=height;
qpos=list.size();
}
if(pheight!=-1&&qheight!=-1){
int min=Math.min(pheight,qheight);
if(height<=min){
boolean flag=true;
if(list.contains(height)){
int minpos=Math.min(ppos,qpos);
int maxpos=Math.max(ppos,qpos);
for (int i = minpos; i < maxpos; i++) {
if(list.get(i)==height){
flag=false;
break;
}
}
}
if(flag){
parent=root;
height=-1;
}
}
}
list.add(height);
//System.out.println(root.val+" "+height);
return height;
}
}
//递归法
/**
* 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;
}
//其实就是判断每个节点的左孩子和右孩子中有没有找到p和q
//在左孩子中找到了一个,就去右孩子中找,如果右孩子中没有找到,则说明当前节点就是最近公共节点
//同理,同理返回,返回
TreeNode left=lowestCommonAncestor(root.left,p,q);
TreeNode right=lowestCommonAncestor(root.right,p,q);
System.out.println(root.val);
if(left==null&&right==null){
return null;
} else if (left != null && right == null) {
return left;
} else if (left == null && right != null) {
return right;
}else {
return root;
}
}
}