-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0653-Two-sum-IV-input-is-a-BST.cs
More file actions
65 lines (48 loc) · 1.62 KB
/
0653-Two-sum-IV-input-is-a-BST.cs
File metadata and controls
65 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._0653.Two_sum_IV_input_is_a_BST
{
public class _0653_Two_sum_IV_input_is_a_BST
{
// Approach 2
public bool FindTarget(TreeNode root, int k)
{
if (root == null) return false;
List<int> bucket = new List<int>();
return Order(root, k, bucket);
}
private bool Order(TreeNode node, int target, List<int> bucket)
{
if (node == null) return false;
if (bucket.Contains(target - node.val)) return true;
if (!bucket.Contains(node.val)) bucket.Add(node.val);
return Order(node.left, target, bucket) || Order(node.right, target, bucket);
}
// Approach 1
//public bool FindTarget(TreeNode root, int k)
//{
// if (root == null) return false;
// List<int> bucket = new List<int>();
// Fill(root, bucket);
// List<int> box = new List<int>();
// box.Add(bucket[0]);
// int diff = 0;
// for (int i = 1; i < bucket.Count; i++)
// {
// diff = k - bucket[i];
// if (box.Contains(diff)) return true;
// if (!box.Contains(bucket[i])) box.Add(bucket[i]);
// }
// return false;
//}
//private void Fill(TreeNode node, List<int> bucket)
//{
// if (node == null) return;
// bucket.Add(node.val);
// Fill(node.left, bucket);
// Fill(node.right, bucket);
//}
}
}