代码随想录算法训练营第16天|530. 二叉搜索树的最小绝对差、501. 二叉搜索树中的众数、236. 二叉树的最近公共祖先

530. 二叉搜索树的最小绝对差

注意是二叉搜索树。
最直观的做法,就是中序遍历二叉搜索树,最小差值一定出现在中序遍历序列的相邻元素之间。

class Solution {
	TreeNode preNode;
	int res = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
		inOrder(root);
		return res;
    }

	//中序遍历二叉搜索树
	public void inOrder(TreeNode root){
		if(root == null){
			return;
		}
		inOrder(root.left);

		if(preNode != null){
			res = Math.min(res, root.val - preNode.val);
		}
		preNode = root;
		inOrder(root.right);
	}
}

——————————————————————————————————————————

501. 二叉搜索树中的众数

中序遍历,使用一个count记录节点值的出现次数,当当前节点和上一个节点的值相等时,count+1,否则count重置为1。
每遍历一个节点,判断一下count和以前记录的最大次数nowMaxCount的大小,若count更大,则将结果集合中的所有元素移除,并将上一个节点的元素加入集合
若nowMaxCount更大,不做操作。若一样大,则将上一个节点的值加入集合

class Solution {
	List<Integer> resList = new ArrayList<>();
	int nowMaxCount = 1;
	int count = 1;

	TreeNode preNode;
    public int[] findMode(TreeNode root) {
		inOrderFindFunc(root);
		return resList.stream()
				.filter(Objects::nonNull)
				.mapToInt(value -> value)
				.toArray();
		}

	private void inOrderFindFunc(TreeNode root){
		if(root == null){
			return;
		}

		inOrderFindFunc(root.left);
		if(preNode != null){
			if(root.val == preNode.val){
				count++;
			}else {
				count = 1;
			}
			if(count > nowMaxCount){
				resList.clear();
				resList.add(root.val);
				nowMaxCount = count;
			} else if (count == nowMaxCount) {
				resList.add(root.val);
			}
		}else {
			resList.add(root.val);
		}
		preNode = root;
		inOrderFindFunc(root.right);
	}
}

——————————————————————————————————————————

236. 二叉树的最近公共祖先

 

 自己思考的版本,逻辑有点混乱

class Solution {
	//肯定要遍历二叉树,先确定一下用哪种遍历方式。要先遍历到p、q节点,再遍历到祖先节点,那么应该用后序遍历
	//然后确定一下中间节点如何处理

	boolean findCommonAncestor = false;
	TreeNode res;
	public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
		lowestCommonAncestorFunc(root, p , q);
		return res;
	}

	private TreeNode lowestCommonAncestorFunc(TreeNode root, TreeNode p, TreeNode q) {
		//后序遍历
		//使用一个布尔变量findCommonAncestor,来标识是否已经找到了近公共祖先。找到最近公共祖先后,findCommonAncestor就置为true。
		//p或者q即为最近公共祖先的情况:遍历到p、q任一节点时,查看当前节点的左右子树是否已经遍历到了另一个节点,如果遍历到了,就将结果节点置为当前节点
		//p、q不为最近公共祖先的情况:对中间节点的处理就是,当左右子树的返回值都不为空,说明p和q分别在当前节点的左右字数上,此时若使用一个布尔变量findCommonAncestor还为false,即说明当前节点即为最近公共祖先,将结果节点置为当前节点。
		//当前返回值的情况:当左子树的返回值不为空时,返回左子树的返回值。当右子树的返回值不为空时,返回右子树的返回值。
		//其余情况返回null
		if(root == null){
			return null;
		}

		TreeNode leftNode = lowestCommonAncestorFunc(root.left, p, q);
		TreeNode rightNode = lowestCommonAncestorFunc(root.right, p, q);
		if(root.val == p.val){
			if(leftNode != null || rightNode != null) {
				findCommonAncestor = true;
				res = root;
			}
			return root;
		}
		if(root.val == q.val){
			if(leftNode != null || rightNode != null) {
				findCommonAncestor = true;
				res = root;
			}
			return root;
		}
		if(leftNode != null && rightNode != null && !findCommonAncestor){
			findCommonAncestor = true;
			res = root;
			return root;
		}
		if(leftNode != null){
			return leftNode;
		}
		if(rightNode != null){
			return rightNode;
		}
		return null;
	}
}

代码随想录的解法

 

class Solution {
	public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
		//后序遍历
		//重新整理一下逻辑
		//中间节点的处理方案:当左右子树的返回值都不为null时,说明找到了最近公共祖先,返回当前节点root
		//当左右子树的返回值有一个不为null时,返回不为null的节点
		//当左右子树的返回值都为null时,返回null
		//递归终止条件:当root == null或者p == root或者q == root时,返回当前节点
		if(root == null || root.val == p.val || root.val == q.val){
			return root;//这里包含了p、q其中一个节点为最近公共祖先的情况
		}

		TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
		TreeNode rightNode = lowestCommonAncestor(root.right, p, q);

		if(leftNode == null && rightNode == null){
			return null;
		} else if (leftNode == null && rightNode != null) {
			return rightNode;
		} else if (leftNode != null && rightNode == null) {
			return leftNode;
		}else {
			return root;
		}
	}
}

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值