题目链接

https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/

题目描述

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

案例 1:

输入:
5
/ \
3 6
/ \ \
2 4 7 Target = 9 输出: True

案例 2:

输入:
5
/ \
3 6
/ \ \
2 4 7 Target = 28 输出: 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) {
return find(root, k, root);
} public boolean find(TreeNode root, int k, TreeNode curr) {
if (curr == null) {
return false;
} return find(root, k, curr.left) || find(root, k, curr.val) || find(root, k, curr.right);
} public boolean find(TreeNode root, int k, int currVal) {
if (root == null) { return false; }
int target = k - currVal;
if (target == currVal) { return false; }
if (root.val == target) { return true; }
else if (root.val > target) return find(root.left, k, currVal);
else return find(root.right, k, currVal);
}
}

Leetcode 653. 两数之和 IV - 输入 BST的更多相关文章

  1. LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)

    653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...

  2. Java实现 LeetCode 653 两数之和 IV - 输入 BST(递归,找差值)

    653. 两数之和 IV - 输入 BST 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true. 案例 1: 输入: 5 / \ 3 6 / ...

  3. 653. 两数之和 IV - 输入 BST + HashSet

    653. 两数之和 IV - 输入 BST 题目描述 题解分析 最简单的方法就是遍历整棵树,找出所有可能的组合,判断是否存在和为 kk 的一对节点.现在在此基础上做一些改进. 如果存在两个元素之和为 ...

  4. [Swift]LeetCode653. 两数之和 IV - 输入 BST | 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 s ...

  5. LeetCode653. 两数之和 IV - 输入 BST

    题目 直接暴力 1 class Solution { 2 public: 3 vector<int>ans; 4 bool findTarget(TreeNode* root, int k ...

  6. Java实现 LeetCode 167 两数之和 II - 输入有序数组

    167. 两数之和 II - 输入有序数组 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必 ...

  7. Leetcode 167. 两数之和 II - 输入有序数组 By Python

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...

  8. LeetCode 167. 两数之和 II - 输入有序数组

    题目: 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的 ...

  9. C#LeetCode刷题之#653-两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4098 访问. 给定一个二叉搜索树和一个目标结果,如果 BST 中 ...

随机推荐

  1. git 推送代码到远程端

    git init git add . git commit -m "first commit" git remote add origin "地址" git p ...

  2. ztree树形图自定义图标在jeecg框架中不显示

    有时候工作遇到问题,就会硬着头皮去解决,今天给大家说一个ztree树形图自定义图标在jeecg框架中不显示的解决方法 对于这个问题,官方观法说法是在节点元素中加入icon的字段,然后后跟图标的url, ...

  3. Python输入与循环

    python while循环 while 语句: 执行语句 结束条件 #应用while输出1到11 counts = 1 while True: print("counts:", ...

  4. Ehcache的配置与使用

    Ehcache是JAVA内制的一个缓存框架! 目的:缓解频繁读取数据库的压力; 初步配置如下: <?xml version="1.0" encoding="UTF- ...

  5. js01

    /////////////////////////////////////////////////////////////js开端/////////////////////////////////// ...

  6. 1923. Scary Politics (timus) (dfs) search

    http://acm.timus.ru/problem.aspx?space=1&num=1923 -- timus This is s problem about thd dfs and s ...

  7. Delphi7 企业版安装记录

    Borland Delphi Enterprise Version 7.0[Build 4.453] 云盘下载: 链接:http://pan.baidu.com/s/1gff6Fuz     密码:z ...

  8. 【BZOJ5212】[ZJOI2018] 历史(LCT大黑题)

    点此看题面 大致题意: 给定一棵树每个节点\(Access\)的次数,求最大虚实链切换次数,带修改. 什么是\(Access\)? 推荐你先去学一学\(LCT\)吧. 初始化(不带修改的做法) 首先考 ...

  9. python 的矩阵运算——numpy

    nbarray对象,就类似于C语言的数组!!! 一维数组: nbarray.array([]) 二维数组: nbarray.array([[],[]]) 数组大小: .shape 修改数组的排列: . ...

  10. matlab中padarray函数在numpy、python中的实现

    a = np.arange(6) a = a.reshape((2, 3)) print np.lib.pad(a, 1, 'symmetric') 运行结果: [[ ] [ ] [ ] [ ]]