标签:root false als 节点 mod discus rem sea class
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看的其他coder给出的答案,很详细。
/** * 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) { HashSet<Integer> set = new HashSet(); return dfs(root,set,k); } public boolean dfs(TreeNode root, HashSet<Integer> set ,int k) { if(root == null) return false; if(set.contains(k - root.val)) return true; set.add(root.val); return dfs(root.left, set, k) || dfs(root.right, set, k); } }
public boolean findTarget(TreeNode root, int k) { List<Integer> nums = new ArrayList<>(); inorder(root, nums); for(int i = 0, j = nums.size()-1; i<j;){ if(nums.get(i) + nums.get(j) == k)return true; if(nums.get(i) + nums.get(j) < k)i++; else j--; } return false; } public void inorder(TreeNode root, List<Integer> nums){ if(root == null)return; inorder(root.left, nums); nums.add(root.val); inorder(root.right, nums); }
653. Two Sum IV - Input is a BST
标签:root false als 节点 mod discus rem sea class
原文地址:http://www.cnblogs.com/wxshi/p/7598374.html