-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinaryTreeRightSideView.js
33 lines (29 loc) · 1003 Bytes
/
binaryTreeRightSideView.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
function traversal(pointer, arr, horizontalDist, levelHeight){
if(!pointer) return;
else {
if(!arr.hasOwnProperty(levelHeight)) {
arr[levelHeight] = [horizontalDist, pointer.val];
}
traversal(pointer.right, arr, horizontalDist+1, levelHeight+1);
traversal(pointer.left, arr, horizontalDist-1, levelHeight+1);
// else {
// let oldHDist = arr[levelHeight][0];
// if(horizontalDist >= oldHDist) {
// arr[levelHeight] = [horizontalDist, pointer.val];
// }
// }
}
}
var rightSideView = function(root) {
let horizontalDist = 0, levelHeight = 0;
let treeArr = [root]
let arr = [];
// [
// [max -> horizontalDist , value]
// ]
traversal(root, arr, horizontalDist, levelHeight);
console.log(arr);
arr = arr.map((innerArr)=>{return innerArr[1]});
return arr;
};
// https://leetcode.com/problems/binary-tree-right-side-view/