题目
题目要求找到最大的二叉搜索子树,补充一下子树的定义,从节点a开始,a以及a的所有后裔构成一个子树。如果不按照这个子树定义的话,书中给出的例子存在一个节点数为8的二叉树。
解题思路
思路一
判断一个二叉树是不是搜索二叉树一般的方法就是利用中序遍历,如果中序遍历序列是升序则是搜索二叉树,如果将此方法应用到本题,那么需要遍历所有子树。
思路二
判断一个二叉树是否是搜索二叉树第二种方法的步骤如下:
- 先判断其左右子树是否是搜索二叉树,如果不是那么原二叉树不是搜索二叉树
- 如果根节点的值比左子树最大的值小则不是二叉搜索树
- 如果根节点的值比右子树最小的值大则不是二叉搜索树
- 如果以上3个步骤都满足则原二叉树是二叉搜索树
如果将第二种方法应用与本题,那么可以得到一个递归程序
- 如果空节点返回0(返回值代表树的大小)
- 如果是叶子节点返回1
- 递归判断左子树,如果左子树不是二叉搜索树,返回-1
- 递归判断右子树,如果右子不是二叉搜索树,返回-1
- 如果节点的值是否小于左子树的最右节点的值,返回-1
- 如果节点的值大于右子树的最左节点的值,返回-1
代码
public class MaxSearchTree {
//最大搜索二叉树根节点
private TreeNode MAX_SEARCH_TREE;
//最大搜索二叉大小
private int MAX_SEARCH_TREE_SIZE;
public TreeNode getMaxSearchTree(TreeNode root) {
MAX_SEARCH_TREE = null;
MAX_SEARCH_TREE_SIZE = 0;
getMaxSearchTreeCore(root);
return MAX_SEARCH_TREE;
}
/*
* 1、如果空节点返回0(返回值代表树的大小)
* 2、如果是叶子节点返回1
* 3、递归判断左子树,如果左子树不是二叉搜索树,返回-1
* 4、递归判断右子树,如果右子不是二叉搜索树,返回-1
* 5、如果节点的值是否小于左子树的最右节点的值,返回-1
* 6、如果节点的值大于右子树的最左节点的值,返回-1
*/
private int getMaxSearchTreeCore(TreeNode root) {
if (root == null)
return 0;
if (root.left == null && root.right == null) {
if (MAX_SEARCH_TREE_SIZE == 0) {
MAX_SEARCH_TREE = root;
MAX_SEARCH_TREE_SIZE = 1;
}
return 1;
}
int sizeLeft = -1;
int sizeRight = -1;
if ((sizeLeft = getMaxSearchTreeCore(root.left)) < 0
|| (sizeRight = getMaxSearchTreeCore(root.right)) < 0
|| (root.left != null && getMostRightNode(root.left).val > root.val)
|| (root.right != null && getMostLeftNode(root.right).val < root.val))
return -1;
int sizeTotal = sizeLeft + sizeRight + 1;
if (sizeTotal > MAX_SEARCH_TREE_SIZE) {
MAX_SEARCH_TREE = root;
MAX_SEARCH_TREE_SIZE = sizeTotal;
}
return sizeTotal;
}
/*
* 查找搜索二叉树最大节点
*/
private TreeNode getMaxNode(TreeNode root) {
assert root != null;
while (root.right != null)
root = root.right;
return root;
}
/*
* 查找搜索二叉树最小节点
*/
private TreeNode getMinNode(TreeNode root) {
assert root != null;
while (root.left != null)
root = root.left;
return root;
}
}