在这里插入代码片
/**
public class TreeNode {int val = 0;TreeNode left = null;TreeNode right = null;public TreeNode(int val) {this.val = val;}}
*/
public class Solution {public TreeNode head=null;public TreeNode prev=null;public TreeNode Convert(TreeNode pRootOfTree) {if(pRootOfTree==null){return null;}Convert(pRootOfTree.left);if(prev==null){prev=pRootOfTree;head=pRootOfTree;}else{prev.right=pRootOfTree;pRootOfTree.left=prev;prev=pRootOfTree;}Convert(pRootOfTree.right);return head;}
}
在这里插入代码片
/*
public class TreeNode {int val = 0;TreeNode left = null;TreeNode right = null;public TreeNode(int val) {this.val = val;}}
*/
public class Solution {public boolean compare(TreeNode pRoot1,TreeNode pRoot2){if(pRoot1==null&&pRoot2==null){return true;}if(pRoot1==null||pRoot2==null||pRoot1.val!=pRoot2.val){return false;}return compare(pRoot1.left,pRoot2.right)&&compare(pRoot1.right,pRoot2.left);}boolean isSymmetrical(TreeNode pRoot) {return compare(pRoot,pRoot);}
}
给定一棵二叉树,判断其是否是自身的镜像
在这里插入代码片
/*** Definition for singly-linked list.* class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/
public class Solution {public boolean hasCycle(ListNode head) {if(head==null){return true;}ListNode fast=head;ListNode slow=head;while(fast!=null&&slow!=null){ fast=fast.next.next;slow=slow.next;if(fast==slow){return true;} }return false;
}}