-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTToArray.js
More file actions
26 lines (22 loc) · 790 Bytes
/
BSTToArray.js
File metadata and controls
26 lines (22 loc) · 790 Bytes
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
// Convert Binary Search Tree to Array
import {TreeNode} from './l33t/sortedArrayToBST.js';
const btAsArray = [];
const temp = []; // store root node
function BSTToArray(bt, l=1) {
if (l) btAsArray.length=0; // clear arr on first run only
bt.left ? btAsArray.push(bt.left.val) : btAsArray.push(null);
bt.right ? btAsArray.push(bt.right.val) : btAsArray.push(null);
if (bt.left instanceof TreeNode &&
(bt.left.left || bt.left.right)) {
BSTToArray(bt.left, 0);
}
if (bt.right instanceof TreeNode &&
(bt.right.left || bt.right.right)) {
BSTToArray(bt.right, 0);
}
temp[0] = bt.val; // overwrite root nodes
return btAsArray[btAsArray.length - 1] === null ?
temp.concat(btAsArray).slice(0, -1)
: temp.concat(btAsArray);
}
export default BSTToArray;