forked from jianggao0612/Leetcode_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101 - Symmetric Tree - Recursive.java
More file actions
44 lines (38 loc) · 1.21 KB
/
101 - Symmetric Tree - Recursive.java
File metadata and controls
44 lines (38 loc) · 1.21 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
41
42
43
44
/**
* Symmetric Tree
* Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
*
* Recursive implementation
*
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
// Corner case
if (root == null)
return true;
// Corner case
if ((root.left == null) & (root.right == null))
return true;
// Recursive determination
return symmetricTree(root.left, root.right);
}
/**
* Help function to determine whether two given tree are symmtric
* Left child of one tree should equal right child of the other tree
* Recursive
*/
public boolean symmetricTree(TreeNode left, TreeNode right) {
if ((left == null) && (right == null))
return true;
if (((left == null) && (right != null)) || (left != null) && (right == null) || (left.val != right.val))
return false;
return symmetricTree(left.left, right.right) && symmetricTree(left.right, right.left);
}
}