Leetcode Algorithms-101.Symmetric Tree

问题地址

Symmetric Tree - LeetCode

问题描述

0.jpg

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {

public boolean isSymmetric(TreeNode root) {
return root == null || isSymmetricHelp(root.left, root.right);
}

private boolean isSymmetricHelp(TreeNode left, TreeNode right) {
if(left == null || right == null) return left == right;
if(left.val != right.val) return false;
return isSymmetricHelp(left.left, right.right) && isSymmetricHelp(left.right, right.left);
}
}

class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
0%