【DFS】LeetCode 101. 对称二叉树
题目链接
思路
DFS 递归解决
代码
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root == null){
return true;
}
return dfs(root.left, root.right);
}
boolean dfs(TreeNode left, TreeNode right){
if(left == null && right == null){
return true;
}else if((left == null && right != null) || (left != null && right == null)){
return false;
}else if(left.val == right.val){
return dfs(left.left, right.right) && dfs(left.right, right.left);
}
return false;
}
}