- Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathSameTree.cs
More file actions
Latest commit
executable file
·41 lines (38 loc) · 2.34 KB
/
Copy pathSameTree.cs
File metadata and controls
executable file
·41 lines (38 loc) · 2.34 KB
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
// Source : https://leetcode.com/problems/same-tree/
// Author : codeyu
// Date : Thursday, March 9, 2017 11:50:32 PM
/**********************************************************************************
*
*
* Given two binary trees, write a function to check if they are equal or not.
*
*
* Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
*
*
**********************************************************************************/
usingSystem;
usingSystem.Collections.Generic;
usingAlgorithms.Utils;
namespaceAlgorithms
{
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
publicclassSolution100
{
publicstaticboolIsSameTree(TreeNodep,TreeNodeq)
{
if(p==null&&q==null)returntrue;//顺序很重要,先判断
if(p==null||q==null)returnfalse;
if(p.Val!=q.Val)returnfalse;
returnIsSameTree(p.Left,q.Left)&&IsSameTree(p.Right,q.Right);
}
}
}