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.

Example 1:

  1. Input:
  2. 5
  3. / \
  4. 3 6
  5. / \ \
  6. 2 4 7
  7.  
  8. Target = 9
  9.  
  10. Output: True

Example 2:

  1. Input:
  2. 5
  3. / \
  4. 3 6
  5. / \ \
  6. 2 4 7
  7.  
  8. Target = 28
  9.  
  10. Output: False
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public boolean findTarget(TreeNode root, int k) {
  12. if (root == null)
  13. return false;
  14. List<Integer> arr = new ArrayList<Integer>();
  15. toArr(root, arr);
  16. int head = 0, tail = arr.size()-1;
  17. while (head < tail) {
  18. if (arr.get(head)+arr.get(tail) == k)
  19. return true;
  20. else if (arr.get(head)+arr.get(tail) < k) {
  21. head ++;
  22. }
  23. else tail --;
  24. }
  25. return false;
  26. }
  27.  
  28. private void toArr(TreeNode node, List<Integer> arr) {
  29. if (node == null)
  30. return ;
  31. toArr(node.left, arr);
  32. arr.add(node.val);
  33. toArr(node.right, arr);
  34. }
  35.  
  36. }

LeetCode - 653. Two Sum IV - Input is a BST的更多相关文章

  1. [LeetCode] 653. 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 ...

  2. LeetCode 653. 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 ...

  3. LeetCode 653 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 ...

  4. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  5. 【Leetcode_easy】653. Two Sum IV - Input is a BST

    problem 653. Two Sum IV - Input is a BST 参考 1. Leetcode_easy_653. Two Sum IV - Input is a BST; 完

  6. 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 日期 题目地址:ht ...

  7. [LeetCode&Python] Problem 653. 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 ...

  8. 【leetcode】653. Two Sum IV - Input is a BST

    Given the root of a Binary Search Tree and a target number k, return true if there exist two element ...

  9. 653. 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 ...

随机推荐

  1. VisualSVN Server的配置和使用方法

    VisualSVN Server是免费的,而VisualSVN是收费的.VisualSVN是SVN的客户端,和Visual Studio集成在一起, VisualSvn Server是SVN的服务器端 ...

  2. 阿里云部署SSL证书详解

    http://mp.weixin.qq.com/s/NV7Zad4DVEgzG2GCHYJVLw 查找中间证书 为了确保兼容到所有浏览器,我们必须在阿里云上部署中间证书,如果不部署证书,虽然安装过程可 ...

  3. /dev/shm 引起的内存统计问题

    最近,有个同事问我,怎么准确地描述linux系统到底还有多少内存可供我使用.这里不扯内存碎片问题,就说剩余总量. 如下: cat /proc/meminfo MemTotal: 263796812 k ...

  4. javascript属性操作

    属性的读写 //属性添加 var obj = {x:1,y:2}; obj.z = 3; obj["m"] = 4; //属性的读取 var obj = {x: 1, y: 2, ...

  5. TypeError: 'encoding' is an invalid keyword argument for this function

    python 2.7 问题 data_file = open("F:\\MyPro\\data.yaml", "r", encoding='utf-8') 运行 ...

  6. python_tornado_session用户验证

    什么是session? -- Django中带有session,tornado中自己写 -- 逻辑整理 用户请求过来,验证通过,随机生成一个字符串当作value返回给浏览器, 在服务器中用户信息与随机 ...

  7. Oracle Sqlload 导入数据

    sqlload导入数据具有快,简单,无需校验等方便,多说无益 1 首先,oracle数据库要有这么个表,用来接收数据.我这里这个uuid是序列生成的,当然也可以sqlload导入时候分配uuid -- ...

  8. angular中要注意的指令

    1.ng-repeat 遍历集合,给每个元素生成模板实例,每个实例的作用域中可以用一些特殊属性,如下: $index //遍历集合的下标 $first //遍历集合中的第一个对象 $last //遍历 ...

  9. vmware虚拟机无法连接网络

    这是一个老生常谈的问题,而且网上有一套解决方法,最方便快捷的肯定属恢复虚拟网络了 说说我的情况 虚拟机VMware® Workstation 12 Pro centos6.8,克隆,192.168.2 ...

  10. VMware10不能安装64位(linux)系统,提示此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态

    今天下载VM10准备安装Ubuntu14.04,一如既往的进行安装,突然发现出现了问题:此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态,具体如图: 如图中提示可重启电脑进入B ...