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 81351af

Browse files
committed
check mirror tree
1 parent 9ca58ce commit 81351af

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 isMirrorTree(root1, root2) {
10+
if (root1==null && root2==null) return true;
11+
if ((root1 != null && root2 != null) && (root1.data == root2.data))
12+
return (isMirrorTree(root1.leftNode, root2.rightNode) && isMirrorTree(root1.rightNode, root2.leftNode))
13+
return false;
14+
}
15+
16+
//level - 1
17+
let tree = new Node(1);
18+
19+
// level - 2
20+
tree.leftNode = new Node(2);
21+
tree.rightNode = new Node(2);
22+
23+
// level -3
24+
tree.leftNode.leftNode = new Node(3);
25+
tree.leftNode.rightNode = new Node(4);
26+
27+
tree.rightNode.leftNode = new Node(4);
28+
tree.rightNode.rightNode = new Node(3);
29+
30+
// 1
31+
// / \
32+
// 2 2
33+
// / \ / \
34+
// 3 4 4 3
35+
console.log('Tree is Mirror Tree = ', isMirrorTree(tree, tree));

0 commit comments

Comments
(0)

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