-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximumSumBSTinBinaryTree.js
135 lines (111 loc) · 2.86 KB
/
maximumSumBSTinBinaryTree.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
125
126
127
128
129
130
131
132
133
134
135
const util = require('util')
class Node {
constructor(data) {
this.left = null;
this.right = null;
this.data = data;
}
setLeftChild(node) {
this.left = node;
}
setRightChild(node) {
this.right = node;
}
print() {
console.log("data ",this.data);
}
isNodeBw(){
if(this.left && !(this.left.data < this.data)) {
return false;
}
if(this.right && !(this.data < this.right.data)) {
return false;
}
return true;
}
}
let node3 = new Node(1);
let node4 = new Node(2);
let node2 = new Node(3);
node2.setLeftChild(node3);
node2.setRightChild(node4);
let root = new Node(4);
root.setLeftChild(node2);
/** Test case 1
let node8 = new Node(4);
let node9 = new Node(6);
let node6 = new Node(2);
let node7 = new Node(5);
node7.setLeftChild(node8);
node7.setRightChild(node9);
let node3 = new Node(3);
node3.setLeftChild(node6);
node3.setRightChild(node7);
let node4 = new Node(2);
let node5 = new Node(4);
let node2 = new Node(4);
node2.setLeftChild(node4);
node2.setRightChild(node5);
let root = new Node(1);
root.setLeftChild(node2);
root.setRightChild(node3);
*/
//console.log(util.inspect(root, {showHidden: false, depth: null}))
function inOrderTraversal(node) {
if(!node) {
return ;
}
inOrderTraversal(node.left);
node.print();
inOrderTraversal(node.right);
}
//inOrderTraversal(root);
////////////////////////////////
let maxPointer = null;
function getMaxAndCheckBST(node){
let isBst = false, sum = null;
if(!node) {
return {
isBst : isBst,
sum: sum
}
}
let leftChildObj = {
isBst: true,
sum: 0
}
if(node.left) leftChildObj = getMaxAndCheckBST(node.left);
let rightChildObj = {
isBst: true,
sum: 0
};
if(node.right) rightChildObj = getMaxAndCheckBST(node.right);
// leftChildObj.isBST
// leftChildObj.sum
// rightChildObj.isBST
// rightChildObj.sum
//console.log(util.inspect(leftChildObj.isBST , {showHidden: false, depth: null}))
if(leftChildObj.isBst && rightChildObj.isBst && node.isNodeBw()) {
isBst = true;
sum = node.data + leftChildObj.sum + rightChildObj.sum;
}
// else if (leftChildObj.isBst && rightChildObj.isBst) {
// isBst = false;
// sum = Math.max(leftChildObj.sum , rightChildObj.sum);
// } else if (!leftChildObj.isBst && rightChildObj.isBst) {
// }
if(isBst) {
if(maxPointer == null) {
maxPointer = sum;
} else {
maxPointer = Math.max(maxPointer, sum);
}
}
return {
isBst : isBst,
sum: sum
}
}
console.log(util.inspect(root, {showHidden: false, depth: null}))
console.log(getMaxAndCheckBST(root))
console.log("maxPointer", maxPointer)