// 层次遍历
public static boolean travel(TreeNode root,int tall) {
String s = "";
int height = 0;
if (root != null) {
LinkedList<TreeNode> list = new LinkedList<TreeNode>();
list.add(root);
TreeNode cur = new TreeNode(0);
TreeNode last = root;
while (!list.isEmpty() && height != tall) {
cur = list.poll();
if (cur.val == Integer.MAX_VALUE) {
s += "*";
} else {
s += cur.val;
//空节点会是层数出错,因此要变化
//空节点设置为值最大的节点
if(cur.left == null){
list.offer(new TreeNode(Integer.MAX_VALUE));
}else{
list.offer(cur.left);
}
if(cur.right == null){
list.offer(new TreeNode(Integer.MAX_VALUE));
}else{
list.offer(cur.right);
}
}
if(cur == last){
last = list.getLast();
height++;
System.out.println(s+f(s));
if(!f(s)){
return false;
}
s = "";
}
}
}
// System.out.println("height = "+height);
return true;
}
public static boolean f(String s) {
int i = 0;
int j = s.length() - 1;
// 0,1 2,3 4 5 6,
while (i < j) {
if (s.charAt(i) != s.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
public static boolean isSymmetric(TreeNode root) {
if(root == null){
return true;
}
return travel(root,getTall(root));
}
public static int getTall(TreeNode root){
if(root == null){
return 0;
}
return Math.max(getTall(root.left), getTall(root.right)) + 1;
}