原题:

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:

Input: 
    5
   / \
  3   6
 / \   \
2   4   7

Target = 9

Output: True

Example 2:

Input: 
    5
   / \
  3   6
 / \   \
2   4   7

Target = 28

Output: False

技巧学习:


暴力方法解:

思想方法:

将BST转化成顺序数组 -> #167


注意:

1)import java.util.List。

2)Integer[] a= list.toArray(new Integer[list.size()]); 使用带参数的toArray 避免Object[] 与integer type冲突。

3)处理BST时child node可能为null


LeetCode #653 Two Sum IV - Input is a BST


Time Complexity: O(n)

Space Complexity: O(n)


相关文章:

  • 2022-12-23
  • 2021-06-27
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2021-05-11
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-13
  • 2021-06-02
  • 2022-01-25
  • 2022-12-23
  • 2021-10-14
  • 2021-10-06
  • 2021-05-30
相关资源
相似解决方案