Determine if the binary tree is a search binary tree and complete binary tree _ two fork tree

Source: Internet
Author: User
Tags static class

Import java.util.*;
		Determine if the binary tree is a search binary tree and complete binary tree the public class istree{//two fork tree node definition public static class node{public int value;
		Public Node left;

		public Node right;
		public Node (int data) {this.value=data; }//Determine if Search binary tree (in-sequence traversal increment) public static Boolean Isbst (Node head) {if (head==null) {RE
          Turn true;
          Boolean res=true;
          Node Pre=null;
          Node Cur1=head;
          Node Cur2=null;
              while (cur1!=null) {cur2=cur1.left;
                  	if (Cur2!=null) {while (CUR2.RIGHT!=NULL&&CUR2.RIGHT!=CUR1) {
                  Cur2=cur2.right;
                  	} if (cur2.right==null) {cur2.right=cur1;
                  	Cur1=cur1.left;
                  Continue
                  }else {cur2.right=null;
           }   } if (Pre!=null&&pre.value>cur1.value) {res=false;
              } Pre=cur1;

          Cur1=cur1.right;


	return res; //To determine if it is a complete binary tree/** (1) Sequence traversal of all nodes (2) The current node has a right child, no left child false (3) The current node is not all children have, then the leaf node, otherwise false/P
       Ublic Static Boolean ISCBT (Node head) {if (Head==null) {return true;
       } queue<node>queue=new linkedlist<node> ();
       Boolean leaf=false;
       Node L=null;
       Node R=null;
       Queue.offer (head);
       	  while (!queue.isempty ()) {head=queue.poll ();
       	  L=head.left;
       	  R=head.right; if (leaf&& (l!=null| | r!=null)) | |
       	  (L==null&&r!=null))
       	  {return false;
       	  } if (L!=null) {queue.offer (L);
       	  } if (R!=null) {Queue.offer (R); } else
       	  {leaf=true;


      
    } return true;    public static void Main (String []args) {/** 4 2 6 1
            3 5 */Node Head=new node (4);
            Head.left=new Node (2);
            Head.right=new Node (6);
            Head.left.left=new Node (1);
            Head.left.right=new Node (3);

            Head.right.left=new Node (5);
            System.out.println (head) (ISBST);

	System.out.println (head) (ISCBT); }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.