forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymmetricTree.java
More file actions
37 lines (33 loc) · 1.04 KB
/
SymmetricTree.java
File metadata and controls
37 lines (33 loc) · 1.04 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
package com.examplehub.leetcode.easy;
import com.examplehub.leetcode.TreeNode;
import com.examplehub.strings.PalindromeString;
import java.util.List;
/** https://leetcode.com/problems/symmetric-tree/ */
public class SymmetricTree {
public static boolean solution1(TreeNode root) {
List<Integer> inOrderPath = BinaryTreeInorderTraversal.solution1(root);
StringBuilder builder = new StringBuilder();
for (Integer number : inOrderPath) {
builder.append(number);
}
return PalindromeString.isPalindrome(builder.toString());
}
public static boolean doSolution2(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null) {
return false;
}
if (left.val != right.val) {
return false;
}
return doSolution2(left.left, right.right) && doSolution2(left.right, right.left);
}
public static boolean solution2(TreeNode root) {
if (root == null) {
return true;
}
return doSolution2(root.left, root.right);
}
}