Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e91349e

Browse files
committed
check for ( children sum ==root )
1 parent 5df19a2 commit e91349e

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
class Node {
3+
constructor(data) {
4+
this.data = data;
5+
this.leftNode = this.rightNode = null;
6+
}
7+
}
8+
9+
function checkForChildrenSum(root) {
10+
if (root == null || (root.leftNode == null && root.rightNode == null)) return true;
11+
let sum = 0;
12+
if (root.leftNode) sum = root.leftNode.data;
13+
if (root.rightNode) sum = sum + root.rightNode.data;
14+
return ((root.data == (sum)) && checkForChildrenSum(root.leftNode) && checkForChildrenSum(root.rightNode))
15+
}
16+
17+
18+
//level - 1
19+
let tree = new Node(10);
20+
21+
// level - 2
22+
tree.leftNode = new Node(8);
23+
tree.rightNode = new Node(2);
24+
25+
// level -3
26+
tree.leftNode.leftNode = new Node(3);
27+
tree.leftNode.rightNode = new Node(5);
28+
29+
tree.rightNode.leftNode = new Node(2);
30+
// 10
31+
// / \
32+
// 8 2
33+
// / \ /
34+
// 3 5 2
35+
console.log(' check for children sum = ', checkForChildrenSum(tree));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /