Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions csharp/0101-symmetric-tree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution
{
public bool IsSymmetric(TreeNode root)
{
return Dfs(root.left, root.right);
}

private bool Dfs(TreeNode leftNode, TreeNode rightNode)
{
if (leftNode is null || rightNode is null)
{
return leftNode == rightNode;
}

if (leftNode.val != rightNode.val)
{
return false;
}

return Dfs(leftNode.left, rightNode.right)
&& Dfs(leftNode.right, rightNode.left);
}
}