zoukankan      html  css  js  c++  java
  • 判断一棵树是否是搜索二叉树、判断一棵树是否是完全二叉树

     1 package my_basic.class_4;
     2 
     3 import java.util.Stack;
     4 
     5 import class_04.Code_07_IsBSTAndCBT.Node;
     6 
     7 public class Code_07_IsSBTAndCBT {
     8     public static class Node{
     9         int value;
    10         Node left;
    11         Node right;
    12         public Node(int value) {
    13             this.value = value;
    14         }
    15     }
    16 
    17     /**判断是否是搜索二叉树
    18      * 中序遍历 有序 
    19      * @return
    20      */
    21     public static Boolean isSBT(Node head) {
    22         if (head != null) {
    23             Stack<Node> stack = new Stack<Node>();
    24             int pre = Integer.MIN_VALUE;
    25             while (!stack.isEmpty() || head!=null) {
    26                 if (head!=null) {
    27                     stack.add(head);
    28                     head = head.left;
    29                 }else {
    30                     head = stack.pop();
    31 //                    System.out.print(head.value+" ");
    32                     if (head.value < pre) {
    33                         return false;
    34                     }
    35                     pre = head.value;
    36                     head = head.right;
    37                 }
    38             }
    39         }
    40         return true;
    41     }
    42     
    43     public static void main(String[] args) {
    44         Node head = new Node(4);
    45         head.left = new Node(2);
    46         head.right = new Node(6);
    47         head.left.left = new Node(1);
    48         head.left.right = new Node(3);
    49         head.right.left = new Node(5);
    50 
    51         System.out.println(isSBT(head));
    52     }
    53 }

  • 相关阅读:
    Java学习——HashMap
    git 常用命令
    java 正则表达式
    java 读写文件
    python Shapely 使用指南
    java 删除目录、 文件
    Java maven安装GDAL
    postgresql运维
    (转)媒体格式分析之flv -- 基于FFMPEG
    (转)rtmp协议简单解析以及用其发送h264的flv文件
  • 原文地址:https://www.cnblogs.com/lihuazhu/p/10963880.html
Copyright © 2011-2022 走看看