Skip to content

Commit ed019d6

Browse files
committed
add the JavaScript related practise for NathansunUniversity blog
1 parent affed2d commit ed019d6

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

nathansununiversity_00.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// http://nathansuniversity.appspot.com/
2+
3+
// the Challenge Problem of pre-requisit
4+
5+
var count = function(tree, seq) {
6+
if (tree === null) {
7+
return 0;
8+
}
9+
10+
if (!seq) {
11+
sum = 0;
12+
}
13+
14+
arguments.callee(tree.left, true);
15+
arguments.callee(tree.right, true);
16+
++sum;
17+
18+
return sum;
19+
};
20+
21+
// ++++++++++++++++++++++++++++++++++++++++++++++
22+
23+
var count = function(tree) {
24+
var sum = 0;
25+
26+
(function walkTree(tree) {
27+
if (tree === null) {
28+
return;
29+
}
30+
31+
walkTree(tree.left);
32+
walkTree(tree.right);
33+
34+
++sum;
35+
})(tree);
36+
37+
return sum;
38+
};
39+
40+
// ++++++++++++++++++++++++++++++++++++++++++++++
41+
42+
var count = function(tree) {
43+
if (tree === null) {
44+
return 0;
45+
}
46+
47+
var count = 0;
48+
var queue = [tree];
49+
50+
while(queue.length) {
51+
var node = queue.shift();
52+
++count;
53+
if (node.left) {
54+
queue.push(node.left);
55+
}
56+
57+
if (node.right) {
58+
queue.push(node.right);
59+
}
60+
}
61+
62+
return count;
63+
};
64+

0 commit comments

Comments
 (0)