随笔分类 -  LeetCode

上一页 1 ··· 3 4 5 6 7 8 9 10 11 12 下一页
摘要:深度优先搜索 import java.util.ArrayList; import java.util.List; class Solution { /** * 全局变量存储结果 */ List<String> list = new ArrayList<>(); /** * 哈希表存储数字和字母的对 阅读全文
posted @ 2022-01-10 15:19 振袖秋枫问红叶 阅读(83) 评论(0) 推荐(0)
摘要:递归 class Solution { public TreeNode deleteNode(TreeNode root, int key) { if (root == null){ return root; } /** * 如果大于当前节点,就在左子树寻找;小于则在右子树寻找 * 相等则分三种情况 阅读全文
posted @ 2022-01-05 17:30 振袖秋枫问红叶 阅读(41) 评论(0) 推荐(0)
摘要:中序遍历 class Solution { public boolean isValidBST(TreeNode root) { ArrayList<Integer> list = new ArrayList<>(); inorder(root, list); /** * 中序遍历得到列表,判断列表 阅读全文
posted @ 2022-01-05 16:04 振袖秋枫问红叶 阅读(29) 评论(0) 推荐(0)
摘要:中序遍历 class Solution { public int kthSmallest(TreeNode root, int k) { ArrayList<Integer> list = new ArrayList<>(); inorder(root, list); return list.get 阅读全文
posted @ 2022-01-05 15:16 振袖秋枫问红叶 阅读(15) 评论(0) 推荐(0)
摘要:二分查找 class Solution { public TreeNode sortedArrayToBST(int[] nums) { return sortedArrayToBST(nums, 0, nums.length - 1); } /** * 每次将数组的中间元素作为根节点,这样得到的二 阅读全文
posted @ 2022-01-05 14:51 振袖秋枫问红叶 阅读(42) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { /** * 因为和有可能为负数,因此初始值取无穷小 */ int max = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { maxSum(root); return max; } / 阅读全文
posted @ 2021-12-29 20:05 振袖秋枫问红叶 阅读(38) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { int maxLength = 0; public int longestUnivaluePath(TreeNode root) { depth(root); return maxLength; } /** * 在《104. 二叉树的最大深度》的基础上 阅读全文
posted @ 2021-12-29 10:23 振袖秋枫问红叶 阅读(26) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public int diameterOfBinaryTree(TreeNode root) { if (root == null){ return 0; } /** * 包含根节点的最长路径 */ int containRoot = maxDepth 阅读全文
posted @ 2021-12-28 20:42 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { List<String> list = new LinkedList<>(); public String smallestFromLeaf(TreeNode root) { dfs(root, new String()); /** * 按照字符串的字 阅读全文
posted @ 2021-12-28 15:20 振袖秋枫问红叶 阅读(31) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { /** * 如果找到了p或者q,就返回 */ if (root == null || root 阅读全文
posted @ 2021-12-27 10:26 振袖秋枫问红叶 阅读(35) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public int pathSum(TreeNode root, int targetSum) { /** * 空节点返回0 */ if (root == null){ return 0; } /** * 分两种情况:路径包含该节点和不包含 * 两种 阅读全文
posted @ 2021-12-26 14:05 振袖秋枫问红叶 阅读(27) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public int sumNumbers(TreeNode root) { List<String> list = convertStr(root); int sum = 0; /** * Integer.parseInt()将字符串转为整型 */ 阅读全文
posted @ 2021-12-25 23:54 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { List<List<Integer>> list = new LinkedList<>(); public List<List<Integer>> pathSum(TreeNode root, int targetSum) { dfs(root, ne 阅读全文
posted @ 2021-12-25 22:49 振袖秋枫问红叶 阅读(30) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { List<String> list = new LinkedList<>(); public List<String> binaryTreePaths(TreeNode root) { if (root == null){ return list; } 阅读全文
posted @ 2021-12-25 22:13 振袖秋枫问红叶 阅读(37) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public int sumOfLeftLeaves(TreeNode root) { if (root == null){ return 0; } int sum = 0; /** * 如果根节点有左孩子,且左孩子是叶子节点时,记录其值 */ if 阅读全文
posted @ 2021-12-25 21:27 振袖秋枫问红叶 阅读(12) 评论(0) 推荐(0)
摘要:深度优先搜索 class Solution { public int countNodes(TreeNode root) { if (root == null){ return 0; } return countNodes(root.left) + countNodes(root.right) + 阅读全文
posted @ 2021-12-25 20:16 振袖秋枫问红叶 阅读(29) 评论(0) 推荐(0)
摘要:深度优先搜索(前序遍历) class Solution { public boolean isBalanced(TreeNode root) { if (root == null){ return true; } /** * 自顶向下 * 除了判断左右子树是否是平衡二叉树以外,还要判断左右子树的高度 阅读全文
posted @ 2021-12-25 18:03 振袖秋枫问红叶 阅读(41) 评论(0) 推荐(0)
摘要:递归 class Solution { public ListNode mergeKLists(ListNode[] lists){ return mergeKLists(lists, 0); } public ListNode mergeKLists(ListNode[] lists, int l 阅读全文
posted @ 2021-12-24 16:42 振袖秋枫问红叶 阅读(29) 评论(0) 推荐(0)
摘要:优先队列+Map import java.util.Comparator; import java.util.HashMap; import java.util.PriorityQueue; class Solution { public int[] topKFrequent(int[] nums, 阅读全文
posted @ 2021-12-24 10:23 振袖秋枫问红叶 阅读(42) 评论(0) 推荐(0)
摘要:广度优先搜索+队列 class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> list = new LinkedList<>(); if (root == null){ return list 阅读全文
posted @ 2021-12-23 15:07 振袖秋枫问红叶 阅读(29) 评论(0) 推荐(0)

上一页 1 ··· 3 4 5 6 7 8 9 10 11 12 下一页