一、题目内容
二、题解
解法1:
思路
分别判断
- 完全二叉树
- 层序遍历,null值也入队
- 最终层序遍历的结果,队列中不会有null间隔
搜索二叉树
- 左右递归
- root为左子树的最大值,右子树的最小值
isSearch(root.left,min,root.val) && isSearch(root.right,root.val,max);
代码
public class Solution {/**** @param root TreeNode类 the root* @return bool布尔型一维数组*/public boolean[] judgeIt (TreeNode root) {// write code herereturn new boolean[]{isSearch(root,null,null),isAllTree(root)};}private boolean isSearch(TreeNode root,Integer min,Integer max){if(root == null){return true;}// 如果root值小于最低项则不是搜索二叉树if(min!=null && root.val<=min){return false;}if(max!=null && root.val>=max){return false;}return isSearch(root.left,min,root.val) && isSearch(root.right,root.val,max);}private boolean isAllTree(TreeNode root){if(root == null) {return true;}Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.offer(root);TreeNode left = null, right = null;while(!queue.isEmpty()){TreeNode node = queue.poll();if(node!=null){queue.offer(node.left);queue.offer(node.right);}else{while(!queue.isEmpty()){if(queue.poll()!=null){return false;}}}}return true;}}
