Skip to content

Commit 4044edc

Browse files
committed
is Same SubTree Sln
1 parent 2e4c600 commit 4044edc

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Tree/Tree.TreeLib/IsSameTreeSln.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.Text;
4+
5+
namespace leetcodeTest
6+
{
7+
public class IsSameTreeSln
8+
{
9+
// Definition for a binary tree node.
10+
public class TreeNode
11+
{
12+
public int val;
13+
public TreeNode left;
14+
public TreeNode right;
15+
public TreeNode(int x) { val = x; }
16+
}
17+
18+
public bool IsSubtree(TreeNode s, TreeNode t)
19+
{
20+
if (s == null) return false;
21+
if (isSame(s, t)) return true;
22+
//s.left-tree和t-tree是不是相等树?
23+
//s.right-tree和t-tree是不是相等树?
24+
return IsSubtree(s.left, t) || IsSubtree(s.right, t);
25+
}
26+
//判断s-tree和t-tree是不是相等树
27+
private bool isSame(TreeNode s, TreeNode t)
28+
{
29+
if (s == null && t == null) return true;
30+
if (s == null || t == null) return false;
31+
return s.val == t.val &&
32+
isSame(s.left, t.left) &&
33+
isSame(s.right, t.right);
34+
}
35+
36+
37+
}
38+
}

0 commit comments

Comments
 (0)