Skip to content

Commit df9836e

Browse files
author
haotf
committed
二叉搜索树
1 parent deb945d commit df9836e

9 files changed

+342
-0
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode
2+
log

Diff for: 1038.把二叉搜索树转换为累加树.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @lc app=leetcode.cn id=1038 lang=java
3+
*
4+
* [1038] 把二叉搜索树转换为累加树
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
25+
private int res = 0;
26+
27+
public TreeNode bstToGst(TreeNode root) {
28+
return traverse(root);
29+
}
30+
31+
private TreeNode traverse(TreeNode root) {
32+
if (root == null)
33+
return null;
34+
traverse(root.right);
35+
res = res + root.val;
36+
root.val = res;
37+
traverse(root.left);
38+
return root;
39+
}
40+
}
41+
// @lc code=end

Diff for: 230.二叉搜索树中第k小的元素.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @lc app=leetcode.cn id=230 lang=java
3+
*
4+
* [230] 二叉搜索树中第K小的元素
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
private int rank = 0;
25+
26+
public int kthSmallest(TreeNode root, int k) {
27+
return traverse(root, k).val;
28+
}
29+
30+
private TreeNode traverse(TreeNode root, int k) {
31+
if (root == null)
32+
return null;
33+
TreeNode left = traverse(root.left, k);
34+
if (left != null)
35+
return left;
36+
37+
rank++;
38+
if (rank == k) {
39+
return root;
40+
}
41+
TreeNode right = traverse(root.right, k);
42+
return right;
43+
}
44+
}
45+
// @lc code=end

Diff for: 450.删除二叉搜索树中的节点.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @lc app=leetcode.cn id=450 lang=java
3+
*
4+
* [450] 删除二叉搜索树中的节点
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
public TreeNode deleteNode(TreeNode root, int key) {
25+
return traverse(root, key);
26+
}
27+
28+
private TreeNode traverse(TreeNode root, int key) {
29+
if (root == null)
30+
return null;
31+
if (root.val == key) {
32+
if (root.left == null && root.right == null)
33+
return null;
34+
if (root.left != null && root.right == null)
35+
return root.left;
36+
if (root.right != null && root.left == null)
37+
return root.right;
38+
if (root.left != null && root.right != null) {
39+
TreeNode tmp = root.right;
40+
while (tmp.left != null) {
41+
tmp = tmp.left;
42+
}
43+
tmp.right = deleteNode(root.right, tmp.val);
44+
tmp.left = root.left;
45+
return tmp;
46+
}
47+
}
48+
if (root.val > key) {
49+
root.left = traverse(root.left, key);
50+
} else {
51+
root.right = traverse(root.right, key);
52+
}
53+
return root;
54+
}
55+
}
56+
// @lc code=end

Diff for: 538.把二叉搜索树转换为累加树.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode.cn id=538 lang=java
3+
*
4+
* [538] 把二叉搜索树转换为累加树
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
private int res = 0;
25+
26+
public TreeNode convertBST(TreeNode root) {
27+
return traverse(root);
28+
}
29+
30+
private TreeNode traverse(TreeNode root) {
31+
if (root == null)
32+
return null;
33+
traverse(root.right);
34+
res = res + root.val;
35+
root.val = res;
36+
traverse(root.left);
37+
return root;
38+
}
39+
}
40+
// @lc code=end

Diff for: 652.寻找重复的子树.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
/*
7+
* @lc app=leetcode.cn id=652 lang=java
8+
*
9+
* [652] 寻找重复的子树
10+
*/
11+
12+
// @lc code=start
13+
/**
14+
* Definition for a binary tree node.
15+
* public class TreeNode {
16+
* int val;
17+
* TreeNode left;
18+
* TreeNode right;
19+
* TreeNode() {}
20+
* TreeNode(int val) { this.val = val; }
21+
* TreeNode(int val, TreeNode left, TreeNode right) {
22+
* this.val = val;
23+
* this.left = left;
24+
* this.right = right;
25+
* }
26+
* }
27+
*/
28+
class Solution {
29+
private List<TreeNode> list = new ArrayList<>();
30+
private Map<String, Integer> map = new HashMap<String, Integer>();
31+
32+
public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
33+
traverse(root);
34+
return list;
35+
}
36+
37+
private String traverse(TreeNode root) {
38+
if (root == null)
39+
return "#";
40+
String str = traverse(root.left) + ',' + traverse(root.right) + ',' + root.val;
41+
if (map.getOrDefault(str, 0) == 1) {
42+
list.add(root);
43+
}
44+
map.put(str, map.getOrDefault(str, 0) + 1);
45+
return str;
46+
}
47+
}
48+
// @lc code=end

Diff for: 700.二叉搜索树中的搜索.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode.cn id=700 lang=java
3+
*
4+
* [700] 二叉搜索树中的搜索
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
public TreeNode searchBST(TreeNode root, int val) {
25+
if (root == null)
26+
return null;
27+
if (root.val == val)
28+
return root;
29+
if (root.val > val) {
30+
return searchBST(root.left, val);
31+
} else {
32+
return searchBST(root.right, val);
33+
}
34+
}
35+
}
36+
// @lc code=end

Diff for: 701.二叉搜索树中的插入操作.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @lc app=leetcode.cn id=701 lang=java
3+
*
4+
* [701] 二叉搜索树中的插入操作
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
public TreeNode insertIntoBST(TreeNode root, int val) {
25+
if (root == null)
26+
return new TreeNode(val);
27+
if (root.val > val) {
28+
root.left = insertIntoBST(root.left, val);
29+
} else if (root.val < val) {
30+
root.right = insertIntoBST(root.right, val);
31+
}
32+
return root;
33+
}
34+
}
35+
// @lc code=end

Diff for: 98.验证二叉搜索树.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @lc app=leetcode.cn id=98 lang=java
3+
*
4+
* [98] 验证二叉搜索树
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
class Solution {
24+
public boolean isValidBST(TreeNode root) {
25+
return valid(root, null, null);
26+
}
27+
28+
private boolean valid(TreeNode root, TreeNode max, TreeNode min) {
29+
if (root == null)
30+
return true;
31+
if (max != null && max.val <= root.val)
32+
return false;
33+
if (min != null && min.val >= root.val)
34+
return false;
35+
36+
return valid(root.left, root, min) && valid(root.right, max, root);
37+
}
38+
}
39+
// @lc code=end

0 commit comments

Comments
 (0)