-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
haotf
committed
Feb 16, 2022
1 parent
deb945d
commit df9836e
Showing
9 changed files
with
342 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode | ||
log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* @lc app=leetcode.cn id=1038 lang=java | ||
* | ||
* [1038] 把二叉搜索树转换为累加树 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
|
||
private int res = 0; | ||
|
||
public TreeNode bstToGst(TreeNode root) { | ||
return traverse(root); | ||
} | ||
|
||
private TreeNode traverse(TreeNode root) { | ||
if (root == null) | ||
return null; | ||
traverse(root.right); | ||
res = res + root.val; | ||
root.val = res; | ||
traverse(root.left); | ||
return root; | ||
} | ||
} | ||
// @lc code=end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* @lc app=leetcode.cn id=230 lang=java | ||
* | ||
* [230] 二叉搜索树中第K小的元素 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
private int rank = 0; | ||
|
||
public int kthSmallest(TreeNode root, int k) { | ||
return traverse(root, k).val; | ||
} | ||
|
||
private TreeNode traverse(TreeNode root, int k) { | ||
if (root == null) | ||
return null; | ||
TreeNode left = traverse(root.left, k); | ||
if (left != null) | ||
return left; | ||
|
||
rank++; | ||
if (rank == k) { | ||
return root; | ||
} | ||
TreeNode right = traverse(root.right, k); | ||
return right; | ||
} | ||
} | ||
// @lc code=end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* @lc app=leetcode.cn id=450 lang=java | ||
* | ||
* [450] 删除二叉搜索树中的节点 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
public TreeNode deleteNode(TreeNode root, int key) { | ||
return traverse(root, key); | ||
} | ||
|
||
private TreeNode traverse(TreeNode root, int key) { | ||
if (root == null) | ||
return null; | ||
if (root.val == key) { | ||
if (root.left == null && root.right == null) | ||
return null; | ||
if (root.left != null && root.right == null) | ||
return root.left; | ||
if (root.right != null && root.left == null) | ||
return root.right; | ||
if (root.left != null && root.right != null) { | ||
TreeNode tmp = root.right; | ||
while (tmp.left != null) { | ||
tmp = tmp.left; | ||
} | ||
tmp.right = deleteNode(root.right, tmp.val); | ||
tmp.left = root.left; | ||
return tmp; | ||
} | ||
} | ||
if (root.val > key) { | ||
root.left = traverse(root.left, key); | ||
} else { | ||
root.right = traverse(root.right, key); | ||
} | ||
return root; | ||
} | ||
} | ||
// @lc code=end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* @lc app=leetcode.cn id=538 lang=java | ||
* | ||
* [538] 把二叉搜索树转换为累加树 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
private int res = 0; | ||
|
||
public TreeNode convertBST(TreeNode root) { | ||
return traverse(root); | ||
} | ||
|
||
private TreeNode traverse(TreeNode root) { | ||
if (root == null) | ||
return null; | ||
traverse(root.right); | ||
res = res + root.val; | ||
root.val = res; | ||
traverse(root.left); | ||
return root; | ||
} | ||
} | ||
// @lc code=end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/* | ||
* @lc app=leetcode.cn id=652 lang=java | ||
* | ||
* [652] 寻找重复的子树 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
private List<TreeNode> list = new ArrayList<>(); | ||
private Map<String, Integer> map = new HashMap<String, Integer>(); | ||
|
||
public List<TreeNode> findDuplicateSubtrees(TreeNode root) { | ||
traverse(root); | ||
return list; | ||
} | ||
|
||
private String traverse(TreeNode root) { | ||
if (root == null) | ||
return "#"; | ||
String str = traverse(root.left) + ',' + traverse(root.right) + ',' + root.val; | ||
if (map.getOrDefault(str, 0) == 1) { | ||
list.add(root); | ||
} | ||
map.put(str, map.getOrDefault(str, 0) + 1); | ||
return str; | ||
} | ||
} | ||
// @lc code=end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* @lc app=leetcode.cn id=700 lang=java | ||
* | ||
* [700] 二叉搜索树中的搜索 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
public TreeNode searchBST(TreeNode root, int val) { | ||
if (root == null) | ||
return null; | ||
if (root.val == val) | ||
return root; | ||
if (root.val > val) { | ||
return searchBST(root.left, val); | ||
} else { | ||
return searchBST(root.right, val); | ||
} | ||
} | ||
} | ||
// @lc code=end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* @lc app=leetcode.cn id=701 lang=java | ||
* | ||
* [701] 二叉搜索树中的插入操作 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
public TreeNode insertIntoBST(TreeNode root, int val) { | ||
if (root == null) | ||
return new TreeNode(val); | ||
if (root.val > val) { | ||
root.left = insertIntoBST(root.left, val); | ||
} else if (root.val < val) { | ||
root.right = insertIntoBST(root.right, val); | ||
} | ||
return root; | ||
} | ||
} | ||
// @lc code=end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* @lc app=leetcode.cn id=98 lang=java | ||
* | ||
* [98] 验证二叉搜索树 | ||
*/ | ||
|
||
// @lc code=start | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
public boolean isValidBST(TreeNode root) { | ||
return valid(root, null, null); | ||
} | ||
|
||
private boolean valid(TreeNode root, TreeNode max, TreeNode min) { | ||
if (root == null) | ||
return true; | ||
if (max != null && max.val <= root.val) | ||
return false; | ||
if (min != null && min.val >= root.val) | ||
return false; | ||
|
||
return valid(root.left, root, min) && valid(root.right, max, root); | ||
} | ||
} | ||
// @lc code=end |