随笔分类 - LeetCode
摘要:深度优先搜索 import java.util.ArrayList; import java.util.List; class Solution { /** * 全局变量存储结果 */ List<String> list = new ArrayList<>(); /** * 哈希表存储数字和字母的对
阅读全文
摘要:递归 class Solution { public TreeNode deleteNode(TreeNode root, int key) { if (root == null){ return root; } /** * 如果大于当前节点,就在左子树寻找;小于则在右子树寻找 * 相等则分三种情况
阅读全文
摘要:中序遍历 class Solution { public boolean isValidBST(TreeNode root) { ArrayList<Integer> list = new ArrayList<>(); inorder(root, list); /** * 中序遍历得到列表,判断列表
阅读全文
摘要:中序遍历 class Solution { public int kthSmallest(TreeNode root, int k) { ArrayList<Integer> list = new ArrayList<>(); inorder(root, list); return list.get
阅读全文
摘要:二分查找 class Solution { public TreeNode sortedArrayToBST(int[] nums) { return sortedArrayToBST(nums, 0, nums.length - 1); } /** * 每次将数组的中间元素作为根节点,这样得到的二
阅读全文
摘要:深度优先搜索 class Solution { /** * 因为和有可能为负数,因此初始值取无穷小 */ int max = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { maxSum(root); return max; } /
阅读全文
摘要:深度优先搜索 class Solution { int maxLength = 0; public int longestUnivaluePath(TreeNode root) { depth(root); return maxLength; } /** * 在《104. 二叉树的最大深度》的基础上
阅读全文
摘要:深度优先搜索 class Solution { public int diameterOfBinaryTree(TreeNode root) { if (root == null){ return 0; } /** * 包含根节点的最长路径 */ int containRoot = maxDepth
阅读全文
摘要:深度优先搜索 class Solution { List<String> list = new LinkedList<>(); public String smallestFromLeaf(TreeNode root) { dfs(root, new String()); /** * 按照字符串的字
阅读全文
摘要:深度优先搜索 class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { /** * 如果找到了p或者q,就返回 */ if (root == null || root
阅读全文
摘要:深度优先搜索 class Solution { public int pathSum(TreeNode root, int targetSum) { /** * 空节点返回0 */ if (root == null){ return 0; } /** * 分两种情况:路径包含该节点和不包含 * 两种
阅读全文
摘要:深度优先搜索 class Solution { public int sumNumbers(TreeNode root) { List<String> list = convertStr(root); int sum = 0; /** * Integer.parseInt()将字符串转为整型 */
阅读全文
摘要:深度优先搜索 class Solution { List<List<Integer>> list = new LinkedList<>(); public List<List<Integer>> pathSum(TreeNode root, int targetSum) { dfs(root, ne
阅读全文
摘要:深度优先搜索 class Solution { List<String> list = new LinkedList<>(); public List<String> binaryTreePaths(TreeNode root) { if (root == null){ return list; }
阅读全文
摘要:深度优先搜索 class Solution { public int sumOfLeftLeaves(TreeNode root) { if (root == null){ return 0; } int sum = 0; /** * 如果根节点有左孩子,且左孩子是叶子节点时,记录其值 */ if
阅读全文
摘要:深度优先搜索 class Solution { public int countNodes(TreeNode root) { if (root == null){ return 0; } return countNodes(root.left) + countNodes(root.right) +
阅读全文
摘要:深度优先搜索(前序遍历) class Solution { public boolean isBalanced(TreeNode root) { if (root == null){ return true; } /** * 自顶向下 * 除了判断左右子树是否是平衡二叉树以外,还要判断左右子树的高度
阅读全文
摘要:递归 class Solution { public ListNode mergeKLists(ListNode[] lists){ return mergeKLists(lists, 0); } public ListNode mergeKLists(ListNode[] lists, int l
阅读全文
摘要:优先队列+Map import java.util.Comparator; import java.util.HashMap; import java.util.PriorityQueue; class Solution { public int[] topKFrequent(int[] nums,
阅读全文
摘要:广度优先搜索+队列 class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> list = new LinkedList<>(); if (root == null){ return list
阅读全文

浙公网安备 33010602011771号