Skip to content

Commit e3e9f2b

Browse files
committed
update
1 parent 8b67b71 commit e3e9f2b

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
2+
// recursion method.
3+
4+
class Solution {
5+
public:
6+
void inorder(TreeNode *root, vector<int> & result){
7+
if(root != NULL){
8+
inorder(root->left, result);
9+
result.push_back(root -> val);
10+
inorder(root -> right, result);
11+
}
12+
}
13+
vector<int> inorderTraversal(TreeNode *root) {
14+
vector<int> result;
15+
inorder(root, result);
16+
return result;
17+
}
18+
};
19+
// iteration method
20+
// idea: inorder: left, root, right.
21+
// we push all the left node into the stack the most left one at the top of the stack
22+
// then pop it and go check the right side of the node
23+
class Solution {
24+
public:
25+
26+
vector<int> inorderTraversal(TreeNode *root) {
27+
vector<int> result;
28+
const TreeNode *p = root;
29+
stack<const TreeNode *> cache;
30+
while(p != NULL || !cache.empty()){
31+
if( p != NULL){
32+
cache.push(p);
33+
p = p -> left;
34+
}else{
35+
p = cache.top();
36+
cache.pop();
37+
result.push_back(p->val);
38+
p = p -> right;
39+
}
40+
}
41+
return result;
42+
}
43+
};
44+
// PREORDER TRAVERSAL
45+
// an empty stack
46+
// do the following steps when the stack is not empty
47+
// push root into the stack
48+
// push right child of popped item to the stack
49+
// push left child of popped item to the stack
50+
51+
vector<int> preorderTraversal(TreeNode *root) {
52+
vector<int> result;
53+
TreeNode *p = new TreeNode(-1);
54+
stack<TreeNode *> myStack;
55+
myStack.push(root);
56+
while(!myStack.empty()){
57+
p = myStack.top();
58+
result.push_back(p -> val);
59+
myStack.pop();
60+
if(p -> right) myStack.push(p -> right);
61+
if( p -> left) myStack.push( p -> left);
62+
}
63+
return result;
64+
}
65+
66+
67+
// POSTORDER TRAVERSAL
68+
// Two methods: 1. two stacks 2. one stack
69+
// 1. create two stacks
70+
// 2. push root to the first stack
71+
// repeat the actions until first stack is empty
72+
// 3. pop the root and push into second stack
73+
// 4. push the left and right children of popped item into the first stack
74+
vector<int> postorderTraversal(TreeNode *root) {
75+
vector<int> result;
76+
if( root == NULL) return;
77+
stack<TreeNode *> myStack1;
78+
stack<TreeNode *> myStack2;
79+
myStack1.push(root);
80+
while(!myStack1.empty()){
81+
TreeNode *p = myStack1.top();
82+
result.push_back(p->val);
83+
myStack1.pop();
84+
myStack2.push(p);
85+
if( p -> right) myStack1.push(p -> right);
86+
if( p -> left) myStack1.push(p -> left);
87+
}
88+
std::reverse(result.begin(), result.end());
89+
return result;
90+
}
91+
// one stack method
92+
// push each node twice and compare each time the top of the stack and curr value
93+
94+
vector postorderTraversal(TreeNode *root) {
95+
vector<int> result;
96+
if( root == NULL) return;
97+
stack<TreeNode *> myStack;
98+
myStack.push(root);
99+
myStack.push(root);
100+
while(!myStack.empty()){
101+
TreeNode *curr = myStack.top();
102+
myStack.pop();
103+
if(!myStack.empty() && curr -> val == myStack.top() -> val){
104+
if( curr -> right){
105+
myStack.push( curr -> right);
106+
myStack.push( curr -> right);
107+
}
108+
if(curr -> left){
109+
myStack.push( curr -> left);
110+
myStack.push( curr -> left);
111+
}
112+
}else{
113+
result.push_back(curr -> val);
114+
}
115+
}
116+
return result;
117+
}
118+

OnlineTest.txt

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
ONLINE TEST
2+
3+
Given a non-negative real number a, its square root is a number x, such that x * x = a.
4+
One way to compute a square root is via successive approximation, where one estimate
5+
yields a better estimate.
6+
7+
For example, let's say you are trying to find the square root of 2, and you have
8+
an estimate of 1.5. We'll say a = 2, and x = 1.5. To compute a better estimate,
9+
we'll divide a by x. This gives a new value y = 1.333333... However, we can't just
10+
take this as our next estimate (why not?). We need to average it with the previous
11+
estimate. So our next estimate, xx will be (x + y) / 2, or 1.416666...
12+
13+
Write a function that takes a non-negative real number a, and an epsilon (a
14+
small real number), and returns an approximation of the square root of a.
15+
16+
double square_root(double a, double epsilon) ...
17+
18+
Epsilon determines how accurate the approximation needs to be. The function
19+
should return the first approximation x it obtains that satisfies abs(x*x - a) < epsilon,
20+
where abs(x) is the absolute value of x.
21+
22+
Questions (answer these):
23+
1. Why can't the next estimate, xx, be computed as a / x, where x is the
24+
previous estimate?
25+
2. What happens, in your implementation, if a = 0?
26+
3. What value does your program give for square_root(2, 1e-6)?
27+
28+
29+
double square_root(double a, double epsilon){
30+
double x = first_approximation(a);
31+
double xx = ( x + a / x) /2;
32+
while( std::fabs( xx - x)/ xx > epsilon){
33+
x = xx;
34+
xx = (x + a/x)/2;
35+
}
36+
return xx;
37+
}
38+
double first_approximation(double a){
39+
if( a == 0) return 0;
40+
double x = 0.1;
41+
for(x; x < a; x+= 0.1){
42+
if( x * x > a){
43+
break;
44+
}
45+
}
46+
return x;
47+
}

0 commit comments

Comments
 (0)