[LeetCode] 653. Two Sum IV - Input is a BST

本文介绍了LeetCode上二数之和IV:输入为BST问题的三种解题方法:递归(DFS)、BFS及二叉树遍历。通过实例详细解析了每种方法的实现过程及优缺点,最后总结了适用于不同场景的最佳实践。

原题链接: https://leetcode.com/problems/two-sum-iv-input-is-a-bst/

题目描述

Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.

在这里插入图片描述
在这里插入图片描述

解题思路

LeetCode给出了三种方法,分别是递归(DFS)、BFS和二叉树。
https://leetcode.com/problems/two-sum-iv-input-is-a-bst/solution/

1. 递归

按照DFS的顺序,先找到一个a,然后判断HashSet中是否有等于k-a的b,如果有k-a的话,就到了,否则把a放入这个集合。这里用递归做,优点是代码简单,缺点是Runtime和Memory都比较大。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    	Set<Integer> con= new HashSet();
	
		public boolean findTarget(TreeNode root, int k) {
		if(root == null) {
			return false;
		}
		if(con.contains(k-root.val)) {
			return true;
		}
		else {
			con.add(root.val);
		}	
		return findTarget(root.left,k) || findTarget(root.right,k);
    }
}

2. BFS

就是按照BFS的方式,扫描一遍树。

public class Solution {
	List<Integer> con = new ArrayList();
	Queue < TreeNode > qu = new LinkedList();
	public boolean findTarget(TreeNode root, int k) {
        
        qu.add(root);
		while(qu.isEmpty()!= true) {
			TreeNode a = qu.remove();
			
			if(con.contains(k-a.val)) {
				return true;
			}
			else {
				con.add(a.val);
			}
			
			if(a.left != null) {
				qu.add(a.left);
			}
			if(a.right!=null) {
				qu.add(a.right);
			}
		}
		return false;
    }
}

queue的一些操作

add 增加一个元索 ,如果队列已满,则抛出一个IIIegaISlabEepeplian异常
remove 移除并返回队列头部的元素, 如果队列为空,则抛出一个NoSuchElementException异常
element 返回队列头部的元素, 如果队列为空,则抛出一个NoSuchElementException异常
offer 添加一个元素并返回true 如果队列已满,则返回false
poll 移除并返问队列头部的元素 如果队列为空,则返回null
peek 返回队列头部的元素 如果队列为空,则返回null
put 添加一个元素 如果队列满,则阻塞
take 移除并返回队列头部的元素 如果队列为空,则阻塞

3. 二叉树

二叉树有三种遍历方式:前序、中序、后序遍历。采用中序遍历,会将二叉树变为一个递增的数组。然后再使用二分,在这个递增的数组上用双指针l和r从两头逼近就可以了。

class Solution {
  
	List<Integer> con = new ArrayList();
	public boolean findTarget(TreeNode root, int k) {
        
		inorder(root,con);
		int l = 0;
		int r = con.size()-1;
		while(l < r) {
			int sum = con.get(l) + con.get(r);
			if(sum == k) {return true;}
			else if(sum > k) {r--;}
			else {l++;}
		}
		return false;
    }
	
	public void inorder(TreeNode root,List<Integer> con) {
		if(root == null) {
			return;
		}
		inorder(root.left,con);
		con.add(root.val);
		inorder(root.right,con);
	}
}

参考资料

https://leetcode.com/problems/two-sum-iv-input-is-a-bst/solution/
https://www.cnblogs.com/lemon-flm/p/7877898.html

n-Sum专题相关题目

1. Two Sum
15. 3Sum
16. 3Sum Closest
18. 4Sum
167. Two Sum II - Input array is sorted
560. Subarray Sum Equals K
653. Two Sum IV - Input is a BST

Matlab基于粒子群优化算法及鲁棒MPPT控制器提高光伏并网的效率内容概要:本文围绕Matlab在电力系统优化与控制领域的应用展开,重点介绍了基于粒子群优化算法(PSO)和鲁棒MPPT控制器提升光伏并网效率的技术方案。通过Matlab代码实现,结合智能优化算法与先进控制策略,对光伏发电系统的最大功率点跟踪进行优化,有效提高了系统在不同光照条件下的能量转换效率和并网稳定性。同时,文档还涵盖了多种电力系统应用场景,如微电网调度、储能配置、鲁棒控制等,展示了Matlab在科研复现与工程仿真中的强大能力。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的高校研究生、科研人员及从事新能源系统开发的工程师;尤其适合关注光伏并网技术、智能优化算法应用与MPPT控制策略研究的专业人士。; 使用场景及目标:①利用粒子群算法优化光伏系统MPPT控制器参数,提升动态响应速度与稳态精度;②研究鲁棒控制策略在光伏并网系统中的抗干扰能力;③复现已发表的高水平论文(如EI、SCI)中的仿真案例,支撑科研项目与学术写作。; 阅读建议:建议结合文中提供的Matlab代码与Simulink模型进行实践操作,重点关注算法实现细节与系统参数设置,同时参考链接中的完整资源下载以获取更多复现实例,加深对优化算法与控制系统设计的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值