import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return bool布尔型
*/
public boolean isSymmetric (TreeNode root) {
// write code here
if (root == null || root.left == null && root.right == null) {
return true;
}
return search(root.left, root.right);
}
public boolean search(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) {
return true;
} else if (root1 == null || root2 == null) {
return false;
} else if (root1.val != root2.val) {
return false;
} else {
return search(root1.right, root2.left) && search(root1.left, root2.right);
}
}
}
本题考察的知识点就是判断二叉树的对称结构,所用编程语言是java。
二叉树的轴对称结构需要满足如下条件:
1.只有一个根节点或者空树
2.左子树和右子树节点数相等,且左子树的先遍历根节点,然后遍历左节点,最后遍历右节点,跟右子树的先遍历根节点,然后遍历右子树,最后遍历左子树的顺序一致。

京公网安备 11010502036488号