随笔分类 - 

摘要:我一开始的写法: class Solution { private List<List<Integer>> ans = new ArrayList(); private List<Integer> list = new ArrayList(); private void backtracking(i 阅读全文
posted @ 2022-04-19 13:58 明卿册 阅读(27) 评论(0) 推荐(0) 编辑
摘要:这个东西本质上就是一个搜索+存储,只要把遍历的每一个val存下来就行了。 你能意识到这个东西是对右子树的中序遍历,如果实在不知道怎么写就先写注释。 而且也不一定非要一口气写成一个唯一函数,熟能生巧熟能生巧,先熟再巧。 class Solution { private int num = 0; pub 阅读全文
posted @ 2022-04-19 11:56 明卿册 阅读(20) 评论(0) 推荐(0) 编辑
摘要:二叉树的题目无脑递归做,关键还是在三板斧——确定参数和返回值、终止条件和单层逻辑体 public TreeNode trimBST(TreeNode root, int low, int high) { if(root == null) return null; if(root.val < low) 阅读全文
posted @ 2022-04-18 11:49 明卿册 阅读(34) 评论(0) 推荐(0) 编辑
摘要:最难的就是要理解,通过递归函数返回值完成了新加入节点的父子关系赋值操作 class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { if(root == null) { TreeNode node = new Tr 阅读全文
posted @ 2022-03-06 17:22 明卿册 阅读(20) 评论(0) 推荐(0) 编辑
摘要:利用BST有序的特点即可。 class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null || root == p || root == q 阅读全文
posted @ 2022-03-06 16:54 明卿册 阅读(16) 评论(0) 推荐(0) 编辑
摘要:拿到这个题目首先想到的就是希望能够自底向上, 应该使用回溯,回溯是天然的自底向上。 后序遍历就是天然的回溯,最先处理的一定是叶子节点。 那么要走递归三部曲: 确定返回值和参数 确定终止条件 循环体 确定返回值和参数 其实这道题最基本的想法应该是用boolean来表示root中能否找到p和q,不过可以 阅读全文
posted @ 2022-03-06 16:30 明卿册 阅读(20) 评论(0) 推荐(0) 编辑
摘要:利用BST的中序遍历是有序的这个特点即可,再使用树的双指针即可。 难点在于先处理time,再使用time去更新list。 然后双指针的初始化问题是双指针中经常遇到的问题。 class Solution { private TreeNode pre; private int maxTime = 0, 阅读全文
posted @ 2022-03-06 15:53 明卿册 阅读(26) 评论(0) 推荐(0) 编辑
摘要:显然二叉搜索树中两两间距最小的点一定是对该树进行中序遍历得到的: class Solution { private int ans = Integer.MAX_VALUE; private TreeNode pre = null; public int getMinimumDifference(Tr 阅读全文
posted @ 2022-03-06 15:33 明卿册 阅读(17) 评论(0) 推荐(0) 编辑
摘要:这道题有个大陷阱就是,不能单纯比较根节点和左右两个子节点的关系。 所以需要中序遍历,让每个子节点和它的上一个节点进行对比。 class Solution { TreeNode pre = null; public boolean isValidBST(TreeNode root) { if(root 阅读全文
posted @ 2022-03-06 15:19 明卿册 阅读(24) 评论(0) 推荐(0) 编辑
摘要:利用二叉搜索树的特点即可: class Solution { public TreeNode searchBST(TreeNode root, int val) { if(root==null) return null; if(root.left == null && root.right == n 阅读全文
posted @ 2022-03-06 14:59 明卿册 阅读(13) 评论(0) 推荐(0) 编辑
摘要:几乎是最简单的递归题了。 class Solution { public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { if(root1 == null && root2 == null) return null; else if(roo 阅读全文
posted @ 2022-03-06 14:44 明卿册 阅读(10) 评论(0) 推荐(0) 编辑
摘要:题目链接:https://leetcode-cn.com/problems/maximum-binary-tree/ 模拟题 class Solution { private int maxIndex(int[] nums) { int tmp = Integer.MIN_VALUE,idx =0; 阅读全文
posted @ 2022-03-06 14:18 明卿册 阅读(24) 评论(0) 推荐(0) 编辑
摘要:题目链接:https://leetcode-cn.com/problems/path-sum-ii/submissions/ 这道题需要对整个树进行遍历,对结果的存储可以用参数解决,所以不需要返回值。 class Solution { public List<List<Integer>> pathS 阅读全文
posted @ 2022-03-06 13:40 明卿册 阅读(19) 评论(0) 推荐(0) 编辑
摘要:题目链接: 这是一道典型的,只需要对部分树进行搜索的题目,那么递归函数的返回值不能为void而为true。 还有一个技巧,为了处理方便不用进行求和,而是做减法即可。 递归1.0 class Solution { public boolean hasPathSum(TreeNode root, int 阅读全文
posted @ 2022-03-06 13:15 明卿册 阅读(16) 评论(0) 推荐(0) 编辑
摘要:这道题的递归比迭代要难写,recursion是一道树上带回溯,iteration几乎套层次遍历即可。 1.递归写法 说白了就是要找到深度最大的叶子节点 ?如何保证深度最大的就是最左的?——使用前序遍历 ?为什么使用的是前序遍历而不是更倾向于左边的中序遍历 前序遍历中最先访问到的就是最左边的叶子节点。 阅读全文
posted @ 2022-03-06 11:30 明卿册 阅读(29) 评论(0) 推荐(0) 编辑
摘要:题目链接:https://leetcode-cn.com/problems/sum-of-left-leaves/ 要读懂题目,题目要的是左叶子之和,是左叶子不是做节点! 最开始的写法: class Solution { public int sumOfLeftLeaves(TreeNode roo 阅读全文
posted @ 2022-03-06 09:49 明卿册 阅读(5) 评论(0) 推荐(0) 编辑
摘要:class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> result = new ArrayList(); if(root==null) return result; List<Intege 阅读全文
posted @ 2022-02-26 21:18 明卿册 阅读(23) 评论(0) 推荐(0) 编辑
摘要:class Solution { public boolean isBalanced(TreeNode root) { if(root == null) return true; return getHeight(root)== -1 ? false : true; } private int ge 阅读全文
posted @ 2022-02-26 15:02 明卿册 阅读(20) 评论(0) 推荐(0) 编辑
摘要:class Solution { public boolean isSymmetric(TreeNode root) { if(root == null) return true; return judge(root.left, root.right); } private boolean judg 阅读全文
posted @ 2022-02-25 22:50 明卿册 阅读(17) 评论(0) 推荐(0) 编辑
摘要:翻转这个操作也是需要遍历,遍历无非dfs或者bfs,dfs无非前中后序遍历。这里采用前序遍历或者后序遍历,否则有的会被翻转两次。 最重要的是,翻转的时候传入的参数是root,而非要被翻转的两个节点。 class Solution { public TreeNode invertTree(TreeNo 阅读全文
posted @ 2022-02-25 22:36 明卿册 阅读(11) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示