/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
ListNode font = null;
public boolean isPalindrome(ListNode head) {
font = head;
return check(head);
}
//后序遍历 加上提前return false 真的优美
public boolean check(ListNode head) {
if(head == null){
return true;
}
if(!check(head.next)){
return false;
}
if(head.val != font.val){
return false;
}
font = font.next;
return true;
}
}
判断平衡二叉树,也是后序遍历,到最后往回走,边走边判断,首先判断下面是否合规,这一步也是开启递归,然后判断这层是否合规