# 二叉树中最大的二叉搜索子树的大小

CSDN：二叉树中最大的二叉搜索子树的大小

## 思路1

// 判断以 head 为头的树是否为二叉搜索树，如果是，返回节点个数，如果不是，返回0
public static int getBSTSize(TreeNode head) {
return 0;
}
ArrayList<TreeNode> arr = new ArrayList<>();
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i).value <= arr.get(i - 1).value) {
return 0;
}
}
return arr.size();
}

// 收集中序遍历结果
public static void in(TreeNode head, ArrayList<TreeNode> arr) {
return;
}
}


    public static int maxSubBSTSize1(TreeNode head) {
return 0;
}
if (h != 0) {
return h;
}
// 递归调用，获取左边的最大二叉搜索树的大小和右边最大二叉搜索树大小
// 两者中较大那个，就是答案
}


import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.HashMap;

// https://www.nowcoder.com/questionTerminal/380d49d7f99242709ab4b91c36bf2acc
public class Main {

public static class TreeNode {
public int value;
public TreeNode left;
public TreeNode right;

public TreeNode(int data) {
this.value = data;
}
}

public static int getBSTSize(TreeNode head) {
return 0;
}
ArrayList<TreeNode> arr = new ArrayList<>();
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i).value <= arr.get(i - 1).value) {
return 0;
}
}
return arr.size();
}

public static void in(TreeNode head, ArrayList<TreeNode> arr) {
return;
}
}

public static int maxSubBSTSize1(TreeNode head) {
return 0;
}
if (h != 0) {
return h;
}
}

public static void main(String[] args) throws Exception {
HashMap<Integer, TreeNode> map = new HashMap<>();
int n = Integer.parseInt(params[0]);
int rootVal = Integer.parseInt(params[1]);
// 构建二叉树
TreeNode root = new TreeNode(rootVal);
map.put(rootVal, root);
for (int i = 0; i < n; i++) {
int nodeVal = Integer.parseInt(params[0]);
int leftVal = Integer.parseInt(params[1]);
int rightVal = Integer.parseInt(params[2]);
TreeNode node = map.get(nodeVal);
if (leftVal != 0) {
node.left = new TreeNode(leftVal);
map.put(leftVal, node.left);
}
if (rightVal != 0) {
node.right = new TreeNode(rightVal);
map.put(rightVal, node.right);
}
}
System.out.println(maxSubBSTSize1(root));
}

}


## 思路2

    public static class Info {
public Info(int maxSubBSTSize, int max, int min, boolean isBST) {
this.maxSubBSTSize = maxSubBSTSize;
this.isBST = isBST;
this.max = max;
this.min = min;
}
// 二叉树的最大二叉搜索子树大小
private int maxSubBSTSize;
// 二叉树的最大值是多少
private int max;
// 二叉树的最小值是多少
private int min;
// 二叉树是否是二叉搜索树
private boolean isBST;
}


static Info p(TreeNode head);


        Info left = p(head.left);


        // 到这里，说明 head != null，所以maxSize至少是1
int maxSize = 1;
// max 和 min 先预置为 head.value
// isBST 先设置为 true
boolean isBST = true;
if (left != null) {
// 否则为 false
isBST = left.isBST && left.max < head.value;
max = Math.max(left.max, max);
min = Math.min(left.min, min);
maxSize = Math.max(maxSize, left.maxSubBSTSize);
}
if (right != null) {
// 与left != null 分支注释类似
isBST = isBST && right.isBST && right.min > head.value;
max = Math.max(right.max, max);
min = Math.min(right.min, min);
maxSize = Math.max(maxSize, right.maxSubBSTSize);
}
if (isBST) {
maxSize = Math.max((left != null ? left.maxSubBSTSize : 0) + (right != null ? right.maxSubBSTSize : 0) + 1, maxSize);
}


import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.HashMap;

public class Main {

public static class TreeNode {
public int value;
public TreeNode left;
public TreeNode right;

public TreeNode(int data) {
this.value = data;
}
}

public static int maxSubBSTSize2(TreeNode head) {
return 0;
}
}

public static Info p(TreeNode head) {
return null;
}
int maxSize = 1;
boolean isBST = true;
if (left != null) {
isBST = left.isBST && left.max < head.value;
max = Math.max(left.max, max);
min = Math.min(left.min, min);
maxSize = Math.max(maxSize, left.maxSubBSTSize);
}
if (right != null) {
isBST = isBST && right.isBST && right.min > head.value;
max = Math.max(right.max, max);
min = Math.min(right.min, min);
maxSize = Math.max(maxSize, right.maxSubBSTSize);
}
if (isBST) {
maxSize = Math.max((left != null ? left.maxSubBSTSize : 0) + (right != null ? right.maxSubBSTSize : 0) + 1, maxSize);
}
return new Info(maxSize, max, min, isBST);
}

public static class Info {
public Info(int maxSubBSTSize, int max, int min, boolean isBST) {
this.maxSubBSTSize = maxSubBSTSize;
this.isBST = isBST;
this.max = max;
this.min = min;
}

private int maxSubBSTSize;
private int max;
private int min;
private boolean isBST;
}

public static void main(String[] args) throws Exception {
HashMap<Integer, TreeNode> map = new HashMap<>();
int n = Integer.parseInt(params[0]);
int rootVal = Integer.parseInt(params[1]);
// 构建二叉树
TreeNode root = new TreeNode(rootVal);
map.put(rootVal, root);
for (int i = 0; i < n; i++) {
int nodeVal = Integer.parseInt(params[0]);
int leftVal = Integer.parseInt(params[1]);
int rightVal = Integer.parseInt(params[2]);
TreeNode node = map.get(nodeVal);
if (leftVal != 0) {
node.left = new TreeNode(leftVal);
map.put(leftVal, node.left);
}
if (rightVal != 0) {
node.right = new TreeNode(rightVal);
map.put(rightVal, node.right);
}
}
// System.out.println(maxSubBSTSize1(root));
System.out.println(maxSubBSTSize2(root));
}

}



## 更多

posted @ 2022-10-09 21:11  Grey Zeng  阅读(356)  评论(0编辑  收藏  举报