using System; using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 the root * @return bool布尔型一维数组 */ public List<bool> judgeIt (TreeNode root) { // write code here List<bool> ans=new List<bool>(); ans.Add(IsValidBST(root, int.MinValue,int.MaxValue)); ans.Add(IsCompleteBT(root)); return ans; } public bool IsValidBST(TreeNode root,int min,int max) { if(root==null) return true; if(root.val>=max || root.val<=min) return false; return IsValidBST(root.left, min, root.val) && IsValidBST(root.right, root.val, max); } public bool IsCompleteBT(TreeNode root) { Queue<TreeNode> temp=new Queue<TreeNode>(); temp.Enqueue(root); while(temp.Peek()!=null) { var node=temp.Dequeue(); temp.Enqueue(node.left); temp.Enqueue(node.right); } while(temp.Count!=0 && temp.Peek()==null) temp.Dequeue(); return temp.Count==0; } }
p判断二叉搜索树: 递归判断,不断更新边界
判断完全二叉树: 层序遍历,遇到空节点退出,如果为完全二叉树,那么某一层应该全为空节点,否则不是完全二叉树