-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterviewBit1.java
More file actions
127 lines (105 loc) · 2.57 KB
/
Copy pathInterviewBit1.java
File metadata and controls
127 lines (105 loc) · 2.57 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package dummy;
import java.util.ArrayList;
import java.util.Queue;
import java.util.Stack;
public class InterviewBit1 {
public static void main(String[] args) {
}
public int t2Sum(TreeNode A, int B) {
if(A==null) {
return 0;
}
int val=A.val, want=B-A.val;
if(want<val && rec(A.left, want))
return 1;
else if(rec(A.right, want))
return 1;
if(B<A.val)
return t2Sum(A.left, B);
else {
return (t2Sum(A.left, B) | t2Sum(A.right, B));
}
}
public boolean rec(TreeNode A, int want) {
if(A==null) return false;
if(A.val==want) return true;
else if(A.val>want) return rec(A.left, want);
else return rec(A.right, want);
}
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
class Solution {
TreeNode cur;
Stack<TreeNode> st;
public Solution(TreeNode root) {
st= new Stack<TreeNode>();
cur=root;
}
/** @return whether we have a next smallest number */
public boolean hasNext() {
if(st.empty() && cur==null)
return false;
else
return true;
}
/** @return the next smallest number */
public int next() {
int ans=0;
if(cur==null) {
if(!st.empty()) {
TreeNode temp=st.pop();
ans=temp.val;
cur=temp.right;
return ans;
} else
return -1;
} else {
st.push(cur);
cur=cur.left;
return next();
}
}
}
class Solution1 {
ArrayList<ArrayList<Integer>> all=new ArrayList<ArrayList<Integer>>();
public ArrayList<Integer> al;
public int fmin(TreeNode A, int dis) {
if(A!=null) {
return Math.min( fmin(A.left, dis-1), fmin(A.right, dis+1));
}
else return 0;
}
public int fmax(TreeNode A, int dis) {
if(A!=null) {
return Math.max( fmax(A.left, dis-1), fmax(A.right, dis+1));
}
else return 0;
}
public void add(TreeNode A, int level, int dis) {
Queue<TreeNode> q=new LinkedList;
if(A==null) return;
q.add(A);
while(!q.isEmpty()) {
TreeNode temp= q.poll();
}
if(level==dis) {
al.add(A.val);
}
add(A.left, level, dis-1);
add(A.right, level, dis+1);
}
public ArrayList<ArrayList<Integer>> verticalOrderTraversal(TreeNode A) {
int l=fmin(A, 0);
int r=fmax(A,0);
for(int i=l; i<=r; i++) {
al=new ArrayList<Integer>();
add(A, i, 0);
}
return null;
}
}