我新建的个人博客,欢迎访问:hmilzy.github.io


654.最大二叉树

题目链接:最大二叉树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* 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 constructMaximumBinaryTree(int[] nums) {
return constructMaximumBinaryTree(nums,0,nums.length);
}

public TreeNode constructMaximumBinaryTree(int[] nums,int leftIndex,int rightIndex){
if(rightIndex - leftIndex <= 0){
return null;
}
//if(rightIndex - leftIndex == 1){
// return new TreeNode(nums[leftIndex]);
//}
int maxIndex = leftIndex;
int maxValue = nums[maxIndex];
for(int i = leftIndex + 1;i < rightIndex;i++){
if(nums[i] > maxValue){
maxValue = nums[i];
maxIndex = i;
}
}

TreeNode root = new TreeNode(maxValue);

root.left = constructMaximumBinaryTree(nums,leftIndex,maxIndex);
root.right = constructMaximumBinaryTree(nums,maxIndex + 1,rightIndex);

return root;
}
}

617.合并二叉树

题目链接:合并二叉树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 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 mergeTrees(TreeNode root1, TreeNode root2) {
TreeNode root = new TreeNode();
if(root1 == null){
return root2;
}
if(root2 == null){
return root1;
}
root.val = root1.val + root2.val;
root.left = mergeTrees(root1.left,root2.left);
root.right = mergeTrees(root1.right,root2.right);
return root;
}
}

700.二叉搜索树中的搜索

题目链接:二叉搜索树中的搜索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 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 || root.val == val){
return root;
}
if(root.val > val){
return searchBST(root.left,val);
}else{
return searchBST(root.right,val);
}
*/
if(root == null) {
return null;
}
if(root.val == val) {
return root;
}
if(root.val < val) {
root = searchBST(root.right,val);
}else{
root = searchBST(root.left,val);
}
return root;
}
}

98.验证二叉搜索树

题目链接:验证二叉搜索树

我不理解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* 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 isValidBST(root,Long.MIN_VALUE,Long.MAX_VALUE);
}
public boolean isValidBST(TreeNode root,long lower,long upper){
if(root == null) {
return true;
}
if(root.val <= lower || root.val >= upper){
return false;
}
return isValidBST(root.left,lower,root.val) && isValidBST(root.right,root.val,upper);
}
}