Skip to content

Commit dab908a

Browse files
authored
Create 2764-is-array-a-preorder-of-some-binary-tree.js
1 parent ead9b6e commit dab908a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[][]} nodes
3+
* @return {boolean}
4+
*/
5+
var isPreorder = function(nodes) {
6+
const stack = [];
7+
8+
for (const [curr, parent] of nodes) {
9+
while (stack.length && stack[stack.length - 1] !== parent) {
10+
stack.pop();
11+
}
12+
if (!stack.length && parent !== -1) {
13+
return false;
14+
}
15+
stack.push(curr);
16+
}
17+
18+
return true;
19+
};

0 commit comments

Comments
 (0)