zoukankan      html  css  js  c++  java
  • 判断搜索二叉树与完全二叉树

    package tree;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.Queue;
    
    /**
     * 
    给定一棵二叉树,已经其中没有重复值的节点,请判断该二叉树是否为搜索二叉树和完全二叉树。
     */
    public class judge {
        public static class TreeNode {
            int val = 0;
            TreeNode left = null;
            TreeNode right = null;
    
            public TreeNode(int val) {
                this.val = val;
            }
        }
        public static void main(String[] args) {
           TreeNode tr=new TreeNode(1);
           tr.left=new TreeNode(2);
           tr.right=new TreeNode(3); 
           tr.left.left=new TreeNode(4);
           tr.left.right=new TreeNode(5);
        //    tr.right.left=new TreeNode(6);
        //    tr.right.right=new TreeNode(7);
           judgeIt(tr);
        }
        /**
         * 
         * @param root TreeNode类 the root
         * @return bool布尔型一维数组
         */
        public static boolean[] judgeIt (TreeNode root) {
            // write code here
            if(root==null){
                return new boolean[2];
            }
            boolean []result=new boolean[2];
            result[0]=judgeSearch(root);
            result[1]=judgeComple(root);
            return result;
        }
        public static boolean judgeSearch (TreeNode root) {
            //判断搜索二叉树
            //中序遍历
            //结果升序是搜索二叉树
            ArrayList<Integer> list=new ArrayList<>();
            helper(root,list);
            ArrayList<Integer> result= (ArrayList<Integer>) list.clone();
            Collections.sort(list);
            for(int i=0;i<list.size();i++){
                if(list.get(i)!=result.get(i)){
                    return false;
                }
            }        
            return true;   
        }
        public static void helper (TreeNode root,ArrayList<Integer> list) {
            if(root==null){
                return;
            }
            helper(root.left,list);
            list.add(root.val);
            helper(root.right,list);
            return;
        }
        public static boolean judgeComple(TreeNode root) {
            //判断完全二叉树
            //树为空直接返回false
            //否则层序遍历,如果一个结点左右孩子都不为空,则pop该节点,将其左右孩子入队列;
            //2.1>如果遇到一个结点,左孩子为空,右孩子不为空,则该树一定不是完全二叉树;
            //如果遇到一个结点,左孩子不为空,右孩子为空;或者左右孩子都为空;则该节点之后的队列中的结点都为叶子节点;该树才是完全二叉树,否则就不是完全二叉树
            if(root==null){
                return false;
            }
            Queue<TreeNode> q=new LinkedList<>();
            q.offer(root);
            while(!q.isEmpty()){
                TreeNode t=q.peek();
                int x=q.size();
                if(t.left!=null&&t.right!=null){
                    q.poll();
                    q.offer(t.left);
                    q.offer(t.right);
                }else if(t.left==null&&t.right!=null){
                    return false;
                }else if(t.left!=null&&t.right==null||t.left==null&&t.right==null){
                    q.poll();
                    for(int i=0;i<x;i++){
                        if(q.peek().left!=null||q.peek().right!=null){
                            return false;
                        }
                    }
                    return true;
                }
    
                }
            
            return true;
        }
    
        
    }
    

      

  • 相关阅读:
    Java 中文数字转换为阿拉伯数字
    正则表达式转义符
    git .gitignore详解
    git 陷阱小记
    git log 附加命令归纳
    git 命令归纳版
    《Effective Java》 读书笔记(九)使用try-with-resources 语句替代try-finally
    架构设计 | 接口幂等性原则,防重复提交Token管理
    数据源管理 | OLAP查询引擎,ClickHouse集群化管理
    Java并发编程(04):线程间通信,等待/通知机制
  • 原文地址:https://www.cnblogs.com/jieyi/p/14056822.html
Copyright © 2011-2022 走看看