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 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
Time Complexity: O(n)
Space Complexity: O(n)