题解 | #对称的二叉树#
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.*;
public class Solution {
//原:层序遍历,右是否等于左,根据当前节点用下面的左右节点做判断
boolean isSymmetrical(TreeNode pRoot) {
return recur(pRoot, pRoot);
}
private boolean recur(TreeNode pRoot1, TreeNode pRoot2) {
if(pRoot1 == null && pRoot2 == null)
return true;
if(pRoot1 == null || pRoot2 == null || pRoot1.val != pRoot2.val)
return false;
return recur(pRoot1.left, pRoot2.right) && recur(pRoot1.right, pRoot2.left);
}
}
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.*;
public class Solution {
//原:层序遍历,右是否等于左,根据当前节点用下面的左右节点做判断
boolean isSymmetrical(TreeNode pRoot) {
return recur(pRoot, pRoot);
}
private boolean recur(TreeNode pRoot1, TreeNode pRoot2) {
if(pRoot1 == null && pRoot2 == null)
return true;
if(pRoot1 == null || pRoot2 == null || pRoot1.val != pRoot2.val)
return false;
return recur(pRoot1.left, pRoot2.right) && recur(pRoot1.right, pRoot2.left);
}
}