-
Notifications
You must be signed in to change notification settings - Fork 30
/
SymmetricTree.cs
executable file
·71 lines (68 loc) · 3.29 KB
/
SymmetricTree.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Source : https://leetcode.com/problems/symmetric-tree/
// Author : codeyu
// Date : Thursday, March 9, 2017 11:51:02 PM
/**********************************************************************************
*
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
*
*
* For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
*
* 1
* / \
* 2 2
* / \ / \
* 3 4 4 3
*
*
*
* But the following [1,2,2,null,3,null,3] is not:
*
* 1
* / \
* 2 2
* \ \
* 3 3
*
*
*
*
* Note:
* Bonus points if you could solve it both recursively and iteratively.
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode Right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution101
{
public static bool IsSymmetric(TreeNode root)
{
if (root == null)
return true;
return helper(root.Left, root.Right);
}
public static bool helper(TreeNode root1, TreeNode root2)
{
if (root1 == null && root2 == null)
return true;
if (root1 == null || root2 == null)
return false;
if (root1.Val != root2.Val)
return false;
return helper(root1.Left, root2.Right) && helper(root1.Right, root2.Left);
}
}
}