-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsSameTree.cs
37 lines (30 loc) · 1.01 KB
/
IsSameTree.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//https://leetcode.com/problems/same-tree/
namespace LeetCode.Problems;
public sealed class IsSameTree : ProblemBase
{
[Theory]
[ClassData(typeof(IsSameTree))]
public override void Test(object[] data) => base.Test(data);
protected override void AddTestCases()
=> Add(it => it.ParamTree("[1,2,3]").ParamTree("[1,2,3]").Result(true))
.Add(it => it.ParamTree("[1,2]").ParamTree("[1,null,2]").Result(false))
.Add(it => it.ParamTree("[1,2,1]").ParamTree("[1,1,2]").Result(false));
private bool Solution(TreeNode p, TreeNode q)
{
bool Dfs(TreeNode? left, TreeNode? right)
{
if (left == null && right == null)
{
return true;
}
if (left == null || right == null)
{
return false;
}
return left.val == right.val
&& Dfs(left.left, right.left)
&& Dfs(left.right, right.right);
}
return Dfs(p, q);
}
}