问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4098 访问。

给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。

输入: 

5
   / \
  3   6
 / \   \
2   4   7

Target = 9

输出: True

输入: 

5
   / \
  3   6
 / \   \
2   4   7

Target = 28

输出: False


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.

Input: 

5
   / \
  3   6
 / \   \
2   4   7

Target = 9

Output: True

Input: 

5
   / \
  3   6
 / \   \
2   4   7

Target = 28

Output: False


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4098 访问。

public class Program {public static void Main(string[] args) {var root = new TreeNode(1) {left = new TreeNode(3) {left = new TreeNode(5),right = new TreeNode(7)},right = new TreeNode(9)};var res = FindTarget(root, 10);Console.WriteLine(res);Console.ReadKey();}public static bool FindTarget(TreeNode root, int k) {var nums = new List<int>();PreOrder(root, nums);return TwoSum(nums.ToArray(), k);}public static void PreOrder(TreeNode root, List<int> nums) {if(root == null) return;nums.Add(root.val);PreOrder(root.left, nums);PreOrder(root.right, nums);}public static bool TwoSum(int[] nums, int target) {//用数组中的值做key,索引做value存下所有值var dictionary = new Dictionary<int, int>();for(int i = 0; i < nums.Length; i++) {//记录差值int complement = target - nums[i];//若字典中已经存在这个值,说明匹配成功if(dictionary.ContainsKey(complement)) return true;//记录索引dictionary[nums[i]] = i;}return false;}public class TreeNode {public int val;public TreeNode left;public TreeNode right;public TreeNode(int x) { val = x; }}}

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4098 访问。

true

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#653-两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)相关推荐

  1. leetcode刷题:2.两数之和

    本篇博客介绍如何找到两数之和.获取相加的两个数获取最终target的方法. 先看如下图: 我们很容易想到暴力解法.即两次循环获取结果,第一次循环循环黑圈,我们先假定第一个黑圈中1,是我们要的数,然后接 ...

  2. LeetCode刷题笔记——001两数之和

    题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使用 ...

  3. 【leedcode刷题1】两数之和

    [leedcode刷题 1]两数之和 大家好,小生不才,从今天开始将自己刷题的过程记录在博客中,因为能力有限,所以如果有什么错的地方希望大家积极指正,不胜感激. 题目 给定一个整数数组 nums 和一 ...

  4. LeetCode刷题笔记- 15.三数之和

    LeetCode刷题笔记- 15.三数之和 C语言 题目 注意点 C语言 /*** Return an array of arrays of size *returnSize.* The sizes ...

  5. 【leetcode刷题】21.三数之和——Java版

    ⭐欢迎订阅<leetcode>专栏,每日一题,每天进步⭐ 一题二写,三数之和,题解四瞅五瞄六瞧,水平还七上八下九流,十分辣鸡. --leetcode此题热评 前言 哈喽,大家好,我是一条. ...

  6. 【leetcode】力扣刷题(1):两数之和(Go、Python)

    一.问题描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...

  7. python【力扣LeetCode算法题库】—两数之和

    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元 ...

  8. C#LeetCode刷题之#633-平方数之和( Sum of Square Numbers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3885 访问. 给定一个非负整数 c ,你要判断是否存在两个整数 ...

  9. 【菜菜子力扣刷题】1.两数之和

    题目描述 我的代码 /*** Note: The returned array must be malloced, assume caller calls free().*/ int* twoSum( ...

  10. LeetCode(合集)两数之和总结 (1,167,1346)

    1.两数之和 167. 两数之和 II - 输入有序数组 golang 1346. 检查整数及其两倍数是否存在 一个数组是否存在两个数是target的整数倍? 如果有人知道这是LeetCode哪个题, ...

最新文章

  1. jQuery lazyload 懒加载
  2. 模拟noj——打扑克
  3. PHP中两种包含文件方式、三种注释风格、四种标记风格
  4. Java中Properties类的学习总结
  5. 一加8 Pro相机能“透视”,一加工程师:大家不要“有一个大胆的想法”
  6. Android 上哪个更好:除以 2 还是位移 1?
  7. BSCI—7:OSPF的路由汇总
  8. 基于python的智能风扇设计_[Micropython]TPYBoardV102 DIY智能温控小风扇
  9. C#开发ActiveX控件
  10. xyz坐标转换ybc_GNSS仰角和方位角的计算及代码,XYZ转BLH坐标的代码及原理
  11. adobe安装错误代码183
  12. python while遍历字符串_零基础学Python:while循环和for遍历
  13. 单片机秒表c语言,单片机制作秒表计时器(c语言)
  14. 一些大数据学习书籍pdf
  15. (附源码)计算机毕业设计SSM驾考服务系统
  16. 《提问的艺术》读后感
  17. 【学习总结】企业信息化管理之数据管理发展与探索之路
  18. centos破解root密码
  19. VPython三维仿真(NO.5) 移动模型和调整姿态
  20. 企业发卡系统源码搭建多商户稳定运营版/商户自定义支付/支持API对接

热门文章

  1. Linux查看哪些进程占用较多的cpu、内存和磁盘IO的方法
  2. varnish 高性能方向代理服务器
  3. Leetcode算法题(C语言)9--移动零
  4. Linux——VIM开发C++自动补全(STL、括号)和开发环境配置
  5. LeetCode 496. Next Greater Element I
  6. 【C++学习】C++中的强制转换
  7. 9206 课堂笔记 综合演练 添加数据与非空验证
  8. python-内存中的那些事儿-看懂变量取值的过程
  9. django-django的开发流程
  10. mysql-查看表-修改表-删除表