diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85e7c1d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ diff --git a/Week_02/leetCode1/Solution1.java b/Week_02/leetCode1/Solution1.java new file mode 100644 index 0000000..71c3a90 --- /dev/null +++ b/Week_02/leetCode1/Solution1.java @@ -0,0 +1,28 @@ +class Solution1 { + public int[] twoSum(int[] nums, int target) { + if(nums == null && nums.length < 2){ + return null; + } + + int[] result = new int[2]; + + boolean find = false; + + for(int i = 0;i map = new HashMap<>(); + + for(int i = 0;i< nums.length;i++){ + if(map.containsKey(target - nums[i])){ + return new int[]{i,map.get(target - nums[i])}; + }else{ + map.put(nums[i],i); + } + } + + return null; + } +} \ No newline at end of file diff --git a/Week_02/leetCode1/topic.txt b/Week_02/leetCode1/topic.txt new file mode 100644 index 0000000..b89a768 --- /dev/null +++ b/Week_02/leetCode1/topic.txt @@ -0,0 +1,17 @@ +1.两数之和 +给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 + +你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。 + +  + +示例: + +给定 nums = [2, 7, 11, 15], target = 9 + +因为 nums[0] + nums[1] = 2 + 7 = 9 +所以返回 [0, 1] + +来源:力扣(LeetCode) +链接:https://leetcode-cn.com/problems/two-sum +著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 \ No newline at end of file diff --git a/Week_02/leetCode242/Solution1.java b/Week_02/leetCode242/Solution1.java new file mode 100644 index 0000000..1f61e14 --- /dev/null +++ b/Week_02/leetCode242/Solution1.java @@ -0,0 +1,29 @@ +class Solution1 { + public boolean isAnagram(String s, String t) { + + //hash表 + Map table = new HashMap<>(); + + for(Character c:s.toCharArray()){ + Integer nums = table.get(c) == null?0:table.get(c); + nums++; + table.put(c,nums); + } + + for(Character c:t.toCharArray()){ + Integer nums = table.get(c) == null?0:table.get(c); + nums--; + table.put(c,nums); + } + + Set characters = table.keySet(); + + for(Character c:characters){ + if(table.get(c) != 0){ + return false; + } + } + + return true; + } +} \ No newline at end of file diff --git a/Week_02/leetCode242/Solution2.java b/Week_02/leetCode242/Solution2.java new file mode 100644 index 0000000..592676d --- /dev/null +++ b/Week_02/leetCode242/Solution2.java @@ -0,0 +1,24 @@ +class Solution2 { + public boolean isAnagram(String s, String t) { + + + //用数组方式做hash表的key + int[] table = new int[26]; + + for(Character c:s.toCharArray()){ + table[c-'a'] = table[c-'a'] + 1; + } + + for(Character c:t.toCharArray()){ + table[c-'a'] = table[c-'a'] - 1; + } + + for(int i : table){ + if(i != 0){ + return false; + } + } + + return true; + } +} \ No newline at end of file diff --git a/Week_02/leetCode242/topic.txt b/Week_02/leetCode242/topic.txt new file mode 100644 index 0000000..ff7ff80 --- /dev/null +++ b/Week_02/leetCode242/topic.txt @@ -0,0 +1,20 @@ +242.有效的字母异位词 +给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。 + +示例 1: + +输入: s = "anagram", t = "nagaram" +输出: true +示例 2: + +输入: s = "rat", t = "car" +输出: false +说明: +你可以假设字符串只包含小写字母。 + +进阶: +如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况? + +来源:力扣(LeetCode) +链接:https://leetcode-cn.com/problems/valid-anagram +著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 \ No newline at end of file diff --git a/Week_02/leetCode347/Solution.java b/Week_02/leetCode347/Solution.java new file mode 100644 index 0000000..b4814e0 --- /dev/null +++ b/Week_02/leetCode347/Solution.java @@ -0,0 +1,36 @@ +class Solution { + public int[] topKFrequent(int[] nums, int k) { + if(nums == null || k<=0){ + return null; + } + + Map table = new HashMap<>(); + for (int num : nums) { + table.put(num,table.getOrDefault(num,0)+1); + } + + //用大顶堆, + //堆中元素,为一个长度为2的数组,元素0--原数组中的值;元素1--值在原数组中出现的次数 + PriorityQueue maxHeap = new PriorityQueue<>(new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + return o2[1]-o1[1]; + } + }); + + for (Map.Entry entry : table.entrySet()) { + Integer num = entry.getKey(); + Integer value = entry.getValue(); + + maxHeap.offer(new int[]{num,value}); + } + + int[] res = new int[k]; + + for(int i = 0;i> groupAnagrams(String[] strs) { + if(strs == null || strs.length == 0){ + return null; + } + + List> result= new ArrayList<>(); + + Map> temp = new HashMap<>(); + + int i = 0; + for(String str : strs){ + char[] chars = str.toCharArray(); + Arrays.sort(chars); + String key = Arrays.toString(chars); + List strings = temp.get(key); + if (strings == null){ + strings = new ArrayList<>(); + } + strings.add(str); + temp.put(key,strings); + } + + for(String key : temp.keySet()){ + result.add(temp.get(key)); + } + + + return result; + + } \ No newline at end of file diff --git a/Week_02/leetCode49/topic.txt b/Week_02/leetCode49/topic.txt new file mode 100644 index 0000000..3b6fe8d --- /dev/null +++ b/Week_02/leetCode49/topic.txt @@ -0,0 +1,20 @@ +49. 字母异位词分组 +给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。 + +示例: + +输入: ["eat", "tea", "tan", "ate", "nat", "bat"] +输出: +[ + ["ate","eat","tea"], + ["nat","tan"], + ["bat"] +] +说明: + +所有输入均为小写字母。 +不考虑答案输出的顺序。 + +来源:力扣(LeetCode) +链接:https://leetcode-cn.com/problems/group-anagrams +著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 \ No newline at end of file diff --git a/Week_02/leetCode589/Solution1.java b/Week_02/leetCode589/Solution1.java new file mode 100644 index 0000000..1e97f43 --- /dev/null +++ b/Week_02/leetCode589/Solution1.java @@ -0,0 +1,31 @@ +class Solution { + + //递归 + public List preorder(Node root) { + if(root == null){ + return new ArrayList<>(); + } + + List result = new ArrayList<>(); + + dfs(root,result); + + return result; + } + + private void dfs(Node current, List result) { + if(current == null){ + return; + } + + //前序遍历 根左右 + result.add(current.val); + + if(current.children != null && current.children.size() > 0){ + for(Node child : current.children){ + dfs(child,result); + } + } + + } +} \ No newline at end of file diff --git a/Week_02/leetCode589/topic.md b/Week_02/leetCode589/topic.md new file mode 100644 index 0000000..9d079dd --- /dev/null +++ b/Week_02/leetCode589/topic.md @@ -0,0 +1,11 @@ +给定一个 N 叉树,返回其节点值的*前序遍历*。 + +例如,给定一个 `3叉树` : + + + +![img](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/narytreeexample.png) + + + +返回其前序遍历: `[1,3,5,6,2,4]`。 \ No newline at end of file diff --git a/Week_02/leetCode94/Solution1.java b/Week_02/leetCode94/Solution1.java new file mode 100644 index 0000000..3c2599f --- /dev/null +++ b/Week_02/leetCode94/Solution1.java @@ -0,0 +1,32 @@ +class Solution1 { + //递归 + public List inorderTraversal(TreeNode root) { + + if(root == null){ + return new ArrayList<>(); + } + + List result = new ArrayList<>(); + + dfs(root,result); + + return result; + + } + + private void dfs(TreeNode current, List result) { + if(current == null){ + return; + } + + //中序遍历 左根右 + if(current.left != null){ + dfs(current.left,result); + } + result.add(current.val); + if(current.right != null){ + dfs(current.right,result); + } + + } +} \ No newline at end of file diff --git a/Week_02/leetCode94/Solution2.java b/Week_02/leetCode94/Solution2.java new file mode 100644 index 0000000..0e6c08f --- /dev/null +++ b/Week_02/leetCode94/Solution2.java @@ -0,0 +1,28 @@ +class Solution2 { + public List inorderTraversal(TreeNode root) { + + if(root == null){ + return new ArrayList<>(); + } + + Deque stack = new LinkedList<>(); + List result = new ArrayList<>(); + + while (root != null ||!stack.isEmpty()){ + + while (root != null ){ + stack.push(root); + root = root.left; + } + + root = stack.pop(); + result.add(root.val); + root = root.right; + + } + + + return result; + + } +} \ No newline at end of file diff --git a/Week_02/leetCode94/topic.md b/Week_02/leetCode94/topic.md new file mode 100644 index 0000000..50e33b5 --- /dev/null +++ b/Week_02/leetCode94/topic.md @@ -0,0 +1,40 @@ +#### [94. 二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/) + +给定一个二叉树的根节点 root ,返回它的 中序 遍历。 + + + +示例 1: + +![image-20201222235524261](/Users/waynezhao/Library/Application Support/typora-user-images/image-20201222235524261.png) + +输入:root = [1,null,2,3] +输出:[1,3,2] + +示例 2: + +输入:root = [] +输出:[] +示例 3: + +输入:root = [1] +输出:[1] + +示例 4: + +![image-20201222235600268](/Users/waynezhao/Library/Application Support/typora-user-images/image-20201222235600268.png) + +输入:root = [1,2] +输出:[2,1] +示例 5: + +![image-20201222235614603](/Users/waynezhao/Library/Application Support/typora-user-images/image-20201222235614603.png) + +输入:root = [1,null,2] +输出:[1,2] + + +提示: + +树中节点数目在范围 [0, 100] 内 +-100 <= Node.val <= 100 \ No newline at end of file diff --git "a/Week_02/\350\256\255\347\273\203\345\234\272/topic5/Solution1.java" "b/Week_02/\350\256\255\347\273\203\345\234\272/topic5/Solution1.java" new file mode 100644 index 0000000..73266c0 --- /dev/null +++ "b/Week_02/\350\256\255\347\273\203\345\234\272/topic5/Solution1.java" @@ -0,0 +1,59 @@ +class Solution1 { + //核心思想,借用循环链表的思想,实际上判断两个循环链表是否一致 + public String findSameSnow(int[][] snows) { + if(snows == null || snows.length <=1){ + return "No"; + } + + Set temp = new HashSet<>(); + + for(int[] snow :snows){ + //找到当前雪花中最短的臂长 + int n = 10000000;//题目中臂长范围 0=0;i--){ + left.append(snow[i]).append(",");//加个逗号分隔,避免类似 1,12;11,2这种 + } + + for(int i = snow.length-1;i>= minIndex;i--){ + left.append(snow[i]).append(","); + } + + //遍历后的值放入set中 + //如果不能放,则表明雪花可能相同,返回Yes + if(!temp.add(left.toString())){ + return "Yes"; + } + + StringBuffer right = new StringBuffer(14); + //向右遍历,即以最小值所在的索引为起点,向数组尾部位置遍历; + //到尾部,在从头开始遍历到最小值处 + for(int i = minIndex;i map = new HashMap<>(); + for (int num : nums) { + map.put(num,map.getOrDefault(num,0)+1); + } + + PriorityQueue maxHeap = new PriorityQueue<>(new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + return o2[1] - o1[1]; + } + }); + + for (Map.Entry entry : map.entrySet()) { + Integer num = entry.getKey(); + Integer counts = entry.getValue(); + + maxHeap.offer(new int[]{num,counts}); + } + + int size = maxHeap.size(); + + int finalLength = k preorder.length - 1 || inStart > inEnd) { + return null; + } + TreeNode root = new TreeNode(preorder[preStart]); + int inIndex = 0; // Index of current root in inorder + for (int i = inStart; i <= inEnd; i++) { + if (inorder[i] == root.val) { + inIndex = i; + } + } + root.left = helper(preStart + 1, inStart, inIndex - 1, preorder, inorder); + root.right = helper(preStart + inIndex - inStart + 1, inIndex + 1, inEnd, preorder, inorder); + return root; + } +} \ No newline at end of file diff --git a/Week_03/leetCode105/topic.txt b/Week_03/leetCode105/topic.txt new file mode 100644 index 0000000..abb0302 --- /dev/null +++ b/Week_03/leetCode105/topic.txt @@ -0,0 +1,18 @@ +根据一棵树的前序遍历与中序遍历构造二叉树。 + + 注意: +你可以假设树中没有重复的元素。 + + 例如,给出 + + 前序遍历 preorder = [3,9,20,15,7] +中序遍历 inorder = [9,3,15,20,7] + + 返回如下的二叉树: + + 3 + / \ + 9 20 + / \ + 15 7 + Related Topics 树 深度优先搜索 数组 \ No newline at end of file diff --git a/Week_03/leetCode111/Solution.java b/Week_03/leetCode111/Solution.java new file mode 100644 index 0000000..b939717 --- /dev/null +++ b/Week_03/leetCode111/Solution.java @@ -0,0 +1,23 @@ +class Solution { + public int minDepth(TreeNode root) { + if(root == null){ + return 0; + } + + if(root.left == null && root.right == null){ + return 1; + } + + int min_Depth = Integer.MAX_VALUE; + + if(root.left != null){ + min_Depth = Math.min(minDepth(root.left),min_Depth); + } + + if(root.right != null){ + min_Depth = Math.min(minDepth(root.right),min_Depth); + } + + return min_Depth + 1; + } +} \ No newline at end of file diff --git a/Week_03/leetCode111/topic.txt b/Week_03/leetCode111/topic.txt new file mode 100644 index 0000000..71c810c --- /dev/null +++ b/Week_03/leetCode111/topic.txt @@ -0,0 +1,30 @@ +二叉树的最小深度 +给定一个二叉树,找出其最小深度。 + + 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 + + 说明:叶子节点是指没有子节点的节点。 + + + + 示例 1: + + +输入:root = [3,9,20,null,null,15,7] +输出:2 + + + 示例 2: + + +输入:root = [2,null,3,null,4,null,5,null,6] +输出:5 + + + + + 提示: + + + 树中节点数的范围在 [0, 105] 内 + -1000 <= Node.val <= 1000 \ No newline at end of file diff --git a/Week_03/leetCode22/Solution1.java b/Week_03/leetCode22/Solution1.java new file mode 100644 index 0000000..a3047f9 --- /dev/null +++ b/Week_03/leetCode22/Solution1.java @@ -0,0 +1,25 @@ +class Solution1 { + + private List result = new ArrayList<>(); + + public List generateParenthesis(int n) { + generate(0,0,n,""); + return result; + } + + private void generate(int left, int right, int max, String s) { + if(left == max && right == max){ + result.add(s); + return; + } + + if(left < max){ + generate(left+1,right,max,s+"("); + } + + if(right < left){ + generate(left,right+1,max,s+")"); + } + + } +} \ No newline at end of file diff --git a/Week_03/leetCode22/topic.txt b/Week_03/leetCode22/topic.txt new file mode 100644 index 0000000..7e05033 --- /dev/null +++ b/Week_03/leetCode22/topic.txt @@ -0,0 +1,16 @@ +数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。 + + + + 示例: + + 输入:n = 3 +输出:[ + "((()))", + "(()())", + "(())()", + "()(())", + "()()()" + ] + +Related Topics 字符串 回溯算法 \ No newline at end of file diff --git a/Week_03/leetCode226/Solution1.java b/Week_03/leetCode226/Solution1.java new file mode 100644 index 0000000..27a4998 --- /dev/null +++ b/Week_03/leetCode226/Solution1.java @@ -0,0 +1,15 @@ +class Solution1 { + public TreeNode invertTree(TreeNode root) { + + if(root == null){ + return null; + } + + TreeNode left = invertTree(root.left); + TreeNode right = invertTree(root.right); + root.left = right; + root.right = left; + return root; + + } +} \ No newline at end of file diff --git a/Week_03/leetCode226/Solution2.java b/Week_03/leetCode226/Solution2.java new file mode 100644 index 0000000..5352591 --- /dev/null +++ b/Week_03/leetCode226/Solution2.java @@ -0,0 +1,27 @@ +class Solution { + public TreeNode invertTree(TreeNode root) { + if(root == null){ + return null; + } + + Deque queue = new LinkedList<>(); + queue.push(root); + + while (!queue.isEmpty()){ + TreeNode current = queue.pop(); + TreeNode temp =current.right; + current.right = current.left; + current.left = temp; + + if(current.left != null){ + queue.push(current.left); + } + + if(current.right != null){ + queue.push(current.right); + } + } + + return root; + } +} \ No newline at end of file diff --git a/Week_03/leetCode226/topic.txt b/Week_03/leetCode226/topic.txt new file mode 100644 index 0000000..3d748cc --- /dev/null +++ b/Week_03/leetCode226/topic.txt @@ -0,0 +1,19 @@ +翻转一棵二叉树。 + + 示例: + + 输入: + + 4 + / \ + 2 7 + / \ / \ +1 3 6 9 + + 输出: + + 4 + / \ + 7 2 + / \ / \ +9 6 3 1 \ No newline at end of file diff --git a/Week_03/leetCode236/Solution.java b/Week_03/leetCode236/Solution.java new file mode 100644 index 0000000..bda5ba9 --- /dev/null +++ b/Week_03/leetCode236/Solution.java @@ -0,0 +1,10 @@ +class Solution { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if(root == null || root == p || root == q) return root; + TreeNode left = lowestCommonAncestor(root.left, p, q); + TreeNode right = lowestCommonAncestor(root.right, p, q); + if(left == null) return right; + if(right == null) return left; + return root; + } +} \ No newline at end of file diff --git a/Week_03/leetCode236/topic.txt b/Week_03/leetCode236/topic.txt new file mode 100644 index 0000000..aa3d2e1 --- /dev/null +++ b/Week_03/leetCode236/topic.txt @@ -0,0 +1,27 @@ +236。二叉树的最近公共祖先 +给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 + +百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” + +例如,给定如下二叉树:  root = [3,5,1,6,2,0,8,null,null,7,4] + + + +  + +示例 1: + +输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 +输出: 3 +解释: 节点 5 和节点 1 的最近公共祖先是节点 3。 +示例 2: + +输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 +输出: 5 +解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。 +  + +说明: + +所有节点的值都是唯一的。 +p、q 为不同节点且均存在于给定的二叉树中。 diff --git a/Week_03/leetCode297/Solution.java b/Week_03/leetCode297/Solution.java new file mode 100644 index 0000000..73d9bd6 --- /dev/null +++ b/Week_03/leetCode297/Solution.java @@ -0,0 +1,45 @@ +public class Codec { + + private static final String spliter = ","; + private static final String NN= "#"; + + // Encodes a tree to a single string. + public String serialize(TreeNode root) { + StringBuilder sb = new StringBuilder(); + + buildStr(root,sb); + + return sb.toString(); + } + + private void buildStr(TreeNode root, StringBuilder sb) { + if(root == null){ + sb.append(NN).append(spliter); + }else{ + sb.append(root.val).append(spliter); + buildStr(root.left,sb); + buildStr(root.right,sb); + } + } + + // Decodes your encoded data to tree. + public TreeNode deserialize(String data) { + Deque queue = new LinkedList<>(); + queue.addAll(Arrays.asList(data.split(spliter))); + return buildTree(queue); + } + + private TreeNode buildTree(Deque queue) { + String val = queue.remove(); + + if(val.equals(NN)){ + return null; + }else{ + TreeNode current = new TreeNode(Integer.valueOf(val)); + current.left = buildTree(queue); + current.right = buildTree(queue); + + return current; + } + } +} \ No newline at end of file diff --git a/Week_03/leetCode297/topic.txt b/Week_03/leetCode297/topic.txt new file mode 100644 index 0000000..728d7a6 --- /dev/null +++ b/Week_03/leetCode297/topic.txt @@ -0,0 +1,20 @@ +297. 二叉树的序列化与反序列化 + +序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。 + +请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。 + +示例: + +你可以将以下二叉树: + + 1 + / \ + 2 3 + / \ + 4 5 + +序列化为 "[1,2,3,null,null,4,5]" +提示: 这与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。 + +说明: 不要使用类的成员 / 全局 / 静态变量来存储状态,你的序列化和反序列化算法应该是无状态的。 \ No newline at end of file diff --git a/Week_03/leetCode77/Solution.java b/Week_03/leetCode77/Solution.java new file mode 100644 index 0000000..7a801eb --- /dev/null +++ b/Week_03/leetCode77/Solution.java @@ -0,0 +1,30 @@ +import java.util.ArrayList; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +//leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public List> combine(int n, int k) { + List> res = new ArrayList<>(); + if (k <= 0 || n < k) { + return res; + } + Deque path = new LinkedList<>(); + dfs(n, k, 1, path, res); + return res; + } + + private void dfs(int n, int k, int begin, Deque path, List> res) { + if (path.size() == k) { + res.add(new ArrayList<>(path)); + return; + } + + for (int i = begin; i <= n; i++) { + path.addLast(i); + dfs(n, k, i + 1, path, res); + path.removeLast(); + } + } +} \ No newline at end of file diff --git a/Week_03/leetCode77/topic.txt b/Week_03/leetCode77/topic.txt new file mode 100644 index 0000000..1972735 --- /dev/null +++ b/Week_03/leetCode77/topic.txt @@ -0,0 +1,15 @@ +给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。 + + 示例: + + 输入: n = 4, k = 2 +输出: +[ + [2,4], + [3,4], + [2,3], + [1,2], + [1,3], + [1,4], +] + Related Topics 回溯算法 \ No newline at end of file diff --git a/Week_03/leetCode98/Solution1.java b/Week_03/leetCode98/Solution1.java new file mode 100644 index 0000000..0d7af88 --- /dev/null +++ b/Week_03/leetCode98/Solution1.java @@ -0,0 +1,24 @@ +class Solution { + public boolean isValidBST(TreeNode root) { + Deque stack = new LinkedList<>(); + double min = -Double.MAX_VALUE; + + while (root != null || !stack.isEmpty()){ + while (root != null){ + stack.push(root); + root = root.left; + } + + root = stack.pop(); + + if(root.val <= min){ + return false; + } + + min = root.val; + root = root.right; + } + + return true; + } +} \ No newline at end of file diff --git a/Week_03/leetCode98/Solution2.java b/Week_03/leetCode98/Solution2.java new file mode 100644 index 0000000..a330cd9 --- /dev/null +++ b/Week_03/leetCode98/Solution2.java @@ -0,0 +1,23 @@ +class Solution2 { + + double min = -Double.MAX_VALUE; + + public boolean isValidBST(TreeNode root) { + if(root == null){ + return true; + } + + if(!isValidBST(root.left)){ + return false; + } + + if(root.val <= min){ + return false; + } + + min = root.val; + + return isValidBST(root.right); + + } +} \ No newline at end of file diff --git a/Week_03/leetCode98/topic.txt b/Week_03/leetCode98/topic.txt new file mode 100644 index 0000000..d320be7 --- /dev/null +++ b/Week_03/leetCode98/topic.txt @@ -0,0 +1,32 @@ +给定一个二叉树,判断其是否是一个有效的二叉搜索树。 + + 假设一个二叉搜索树具有如下特征: + + + 节点的左子树只包含小于当前节点的数。 + 节点的右子树只包含大于当前节点的数。 + 所有左子树和右子树自身必须也是二叉搜索树。 + + + 示例 1: + + 输入: + 2 + / \ + 1 3 +输出: true + + + 示例 2: + + 输入: + 5 + / \ + 1 4 +  / \ +  3 6 +输出: false +解释: 输入为: [5,1,4,null,null,3,6]。 +  根节点的值为 5 ,但是其右子节点值为 4 。 + + Related Topics 树 深度优先搜索 递归 \ No newline at end of file