摘要:
//思路:回溯法(先序遍历+路径记录) //关于回溯法的模板网上很多,大家可以去查一下 class Solution { LinkedList<List<Integer>> res = new LinkedList<>(); LinkedList<Integer> path = new Linked 阅读全文
摘要:
//给定一个链表判断是否有环 //思路:快慢指针和HashSet都可以做,前者可以有O(1)较小的空间复杂度,后者至少是O(n) //给出最优的做法 public class Solution{ public boolean hasCycle(ListNode head){ if(head==nul 阅读全文
摘要:
思路:1.首先理解平衡二叉树的定义:任意节点的左右子树的高度差不超过1。 2. class Solution{ public boolean isPin(TreeNode root){ return recur(root)!=-1; } public int recuer(TreeNode root 阅读全文
摘要:
思路:注意recur方法中if(root2==null) return true;的理解,第一次调用recur的时候不会触发该条件的判断。 public class Solution { public boolean HasSubtree(TreeNode root1,TreeNode root2) 阅读全文