653. Two Sum IV - Input is a BST

本文介绍了一种在二叉查找树中寻找两数之和等于特定值k的有效算法。通过中序遍历得到有序数组,再利用双指针技术实现高效搜索。此外还探讨了使用HashSet和广度优先搜索等其他解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两数之和的第四个版本:输入是一颗二叉查找树。


首先讲一下我的解法

二叉查找树的一个重要特点是其中序遍历的结果是一个有序数组。我们可以很好的利用该性质:首先得到BST的中序遍历结果,然后采用167. Two Sum II - Input array is sorted中的方法进行搜索,找到返回true,否则返回false。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean findTarget(TreeNode root, int k) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        inOrder(root,nums);
        //设置两端的指针,进行搜索
        for(int i=0,j=nums.size()-1;i<j;)
        {
            if(nums.get(i) + nums.get(j) == k)  return true;
            else if(nums.get(i) + nums.get(j) > k)  j--;
            else    i++;
        }
        return false;
    }
    //中序遍历得到BST的有序序列
    public void inOrder(TreeNode root,ArrayList<Integer> nums)
    {
        if(root == null) return;
        inOrder(root.left,nums);
        nums.add(root.val);
        inOrder(root.right,nums);
    }
}

这种方法的时间复杂度为O(n),空间复杂度为O(n)。


我们再来看一下leetcode上面给出的solution

1.使用HashSet


2.使用广度优先搜索(BFS)和HashSet


3.利用BST的性质

 这种方法即是我所采用的方法,上面已经给出解答,这里不再详述。

我使用IEEE 的latex检查器,上传文件显示有一个错误和两个警告 This is BibTeX, Version 0.99d (TeX Live 2024) Capacity: max_strings=200000, hash_size=200000, hash_prime=170003 The top-level auxiliary file: output.aux The style file: IEEEtran.bst Reallocated singl_function (elt_size=4) to 100 items from 50. Reallocated singl_function (elt_size=4) to 100 items from 50. Reallocated singl_function (elt_size=4) to 100 items from 50. Reallocated wiz_functions (elt_size=4) to 6000 items from 3000. Reallocated singl_function (elt_size=4) to 100 items from 50. Database file #1: reference.bib Repeated entry---line 360 of file reference.bib : @article{10429768 : , I'm skipping whatever remains of this entry -- IEEEtran.bst version 1.14 (2015/08/26) by Michael Shell. -- http://www.michaelshell.org/tex/ieeetran/bibtex/ -- See the "IEEEtran_bst_HOWTO.pdf" manual for usage information. Done. You've used 30 entries, 4087 wiz_defined-function locations, 995 strings with 12414 characters, and the built_in function-call counts, 26851 in all, are: = -- 2276 > -- 614 < -- 288 + -- 372 - -- 94 * -- 1308 := -- 3935 add.period$ -- 60 call.type$ -- 30 change.case$ -- 30 chr.to.int$ -- 633 cite$ -- 30 duplicate$ -- 1870 empty$ -- 1894 format.name$ -- 120 if$ -- 6326 int.to.chr$ -- 0 int.to.str$ -- 30 missing$ -- 294 newline$ -- 113 num.names$ -- 29 pop$ -- 569 preamble$ -- 1 purify$ -- 0 quote$ -- 2 skip$ -- 2112 stack$ -- 0 substring$ -- 1522 swap$ -- 1702 text.length$ -- 77 text.prefix$ -- 0 top$ -- 5 type$ -- 30 warning$ -- 0 while$ -- 114 width$ -- 32 write$ -- 339 (There was 1 error message)
最新发布
03-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值