forked from TonnyL/Windary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTreeInorderTraversal.js
124 lines (111 loc) · 2.62 KB
/
BinaryTreeInorderTraversal.js
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
/**
* Given a binary tree, return the inorder traversal of its nodes' values.
*
* For example:
* Given binary tree [1,null,2,3],
* 1
* \
* 2
* /
* 3
* return [1,3,2].
*
* Note: Recursive solution is trivial, could you do it iteratively?
*/
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
this.equals = function (node) {
if (this.val !== node.val) {
return false;
}
if (this.left == null) {
if (this.right == null) {
return node.left == null && node.right == null;
}
return this.right.equals(node.right);
}
if (this.right == null) {
return node.right == null;
}
return this.left.equals(node.left) && this.right.equals(node.right);
};
}
/**
* Iterative solution.
*
* @param {TreeNode} root
* @return {number[]}
*/
let inorderTraversal = function (root) {
let list = [];
if (root == null) {
return list;
}
let stack = [];
while (root != null || stack.length !== 0) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack[stack.length - 1];
stack.splice(stack.length - 1);
list.push(root.val);
root = root.right;
}
return list;
};
/**
* Recursive solution.
*
* @param {TreeNode} root
* @return {number[]}
*/
/*let inorderTraversal = function (root) {
let list = [];
if (root == null) {
return list;
}
if (root.left != null) {
inorderTraversal(root.left).forEach(function (i) {
list.push(i);
});
}
list.push(root.val);
if (root.right != null) {
inorderTraversal(root.right).forEach(function (i) {
list.push(i);
});
}
return list;
};*/
if (inorderTraversal(null).length === 0) {
console.log("pass")
} else {
console.error("failed")
}
let node132 = new TreeNode(1);
let right = new TreeNode(2);
right.left = new TreeNode(3);
node132.right = right;
let list132 = [1, 3, 2];
if (inorderTraversal(node132).toString() === list132.toString()) {
console.log("pass")
} else {
console.error("failed")
}
let node4251637 = new TreeNode(1);
let node2 = new TreeNode(2);
let node3 = new TreeNode(3);
node2.left = new TreeNode(4);
node2.right = new TreeNode(5);
node3.left = new TreeNode(6);
node3.right = new TreeNode(7);
node4251637.left = node2;
node4251637.right = node3;
let list4251637 = [4, 2, 5, 1, 6, 3, 7];
if (inorderTraversal(node4251637).toString() === list4251637.toString()) {
console.log("pass")
} else {
console.error("failed")
}