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 a9f4ae3

Browse files
committed
check 2 trees are identical or not
1 parent 924a503 commit a9f4ae3

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
class Node {
3+
constructor(data) {
4+
this.data = data;
5+
this.leftNode = this.rightNode = null;
6+
}
7+
}
8+
9+
function areIdenticalTrees(root1, root2) {
10+
if (root1 == null && root2 == null) return true;
11+
if ((root1 != null && root2 != null) && (root1.data == root2.data))
12+
return (areIdenticalTrees(root1.leftNode, root2.leftNode) && areIdenticalTrees(root1.rightNode, root2.rightNode))
13+
return false;
14+
}
15+
16+
// level - 1
17+
let tree1 = new Node(1);
18+
19+
// level - 2
20+
tree1.leftNode = new Node(2);
21+
tree1.rightNode = new Node(3);
22+
23+
// level - 3
24+
tree1.leftNode.leftNode = new Node(4);
25+
tree1.leftNode.rightNode = new Node(5);
26+
27+
// Tree - 2
28+
// level - 1
29+
let tree2 = new Node(1);
30+
31+
// level - 2
32+
tree2.leftNode = new Node(2);
33+
tree2.rightNode = new Node(3);
34+
35+
// levele -3
36+
tree2.leftNode.leftNode = new Node(4);
37+
tree2.leftNode.rightNode = new Node(5);
38+
39+
// Tree1 and Tree2
40+
// 1
41+
// / \
42+
// 2 3
43+
// / \
44+
// 4 5
45+
console.log('Trees are Identical = ', areIdenticalTrees(tree1, tree2));

0 commit comments

Comments
(0)

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