LeetCode #653 Two Sum IV

来源:互联网 发布:广东省软件企业认定 编辑:程序博客网 时间:2024/06/14 11:37

原题:

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   7Target = 9Output: True

Example 2:

Input:     5   / \  3   6 / \   \2   4   7Target = 28Output: 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




Time Complexity: O(n)

Space Complexity: O(n)


原创粉丝点击