-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution572.cs
41 lines (34 loc) · 973 Bytes
/
Solution572.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
38
39
40
41
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution572
{
/// <summary>
/// 572. Subtree of Another Tree - Easy
/// <a href="https://leetcode.com/problems/subtree-of-another-tree">See the problem</a>
/// </summary>
public bool IsSubtree(TreeNode root, TreeNode subRoot)
{
if (root == null)
{
return false;
}
if (IsSameTree(root, subRoot))
{
return true;
}
return IsSubtree(root.left, subRoot) || IsSubtree(root.right, subRoot);
}
private bool IsSameTree(TreeNode root, TreeNode subRoot)
{
if (root == null && subRoot == null)
{
return true;
}
if (root == null || subRoot == null)
{
return false;
}
return root.val == subRoot.val && IsSameTree(root.left, subRoot.left) && IsSameTree(root.right, subRoot.right);
}
}