Skip to content

Commit 08e3d37

Browse files
committed
VisualStudio: Add symmetry check for binary trees in Solution class
Introduces a new `Solution` class in the `LeetCodeNote` namespace with two static methods: `IsSymmetric` and `IsMirror`. The `IsSymmetric` method determines if a binary tree is symmetric by utilizing the `IsMirror` method, which recursively compares nodes for equality and checks their left and right subtrees. This implementation provides a structured approach to solving the symmetric tree problem.
1 parent 49c441e commit 08e3d37

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

101-110/101.Symmetric Tree.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
8+
namespace LeetCodeNote;
9+
10+
public static partial class Solution
11+
{
12+
public static bool IsSymmetric(TreeNode root)
13+
{
14+
return IsMirror(root.left, root.right);
15+
}
16+
public static bool IsMirror(TreeNode root1, TreeNode root2)
17+
{
18+
19+
if (root1 == null && root2 == null)
20+
{
21+
return true;
22+
}
23+
if (root1 == null || root2 == null)
24+
{
25+
return false;
26+
}
27+
if (root1.val != root2.val)
28+
{
29+
return false;
30+
}
31+
bool left = IsMirror(root1.left, root2.right);
32+
bool right = IsMirror(root1.right, root2.left);
33+
return (left && right);
34+
35+
}
36+
}
37+
38+
? true : false;

0 commit comments

Comments
 (0)