-
Notifications
You must be signed in to change notification settings - Fork 0
/
printAllNodesThatAreAtDistanceKFromALeafNode.js
55 lines (48 loc) · 1.43 KB
/
printAllNodesThatAreAtDistanceKFromALeafNode.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
class Node{
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor() {
this.root = null;
}
printNodesAtDisK(node, k) {
// base cases
debugger
if(!node) return -1;
let left = this.printNodesAtDisK(node.left, k) + 1;
let right = this.printNodesAtDisK(node.right, k) + 1;
if(left == -1 && left == right) return -2;
let maxNodeValue = Math.max(left, right);
if( ( k == left || k == right) ) {
console.log(node.value);
return maxNodeValue;
} else {
return maxNodeValue;
}
}
}
function main() {
let binaryTree = new BinaryTree();
binaryTree.root = new Node(1);
binaryTree.root.left = new Node(2);
binaryTree.root.right = new Node(3);
binaryTree.root.left.left = new Node(4);
binaryTree.root.left.right = new Node(5);
binaryTree.root.right.left = new Node(6);
binaryTree.root.right.right = new Node(7);
binaryTree.root.right.left.right = new Node(8);
let node = binaryTree.root//.right.left;
// console.log(node)
console.log("\n")
for (let itr = 0; itr < 4; itr++) {
binaryTree.printNodesAtDisK(node, itr);
console.log("---")
}
// case is k is larger than height of tree
}
main();
// https://www.geeksforgeeks.org/print-nodes-distance-k-leaf-node/