zoukankan      html  css  js  c++  java
  • 剑指offer--二叉树的后序遍历

    思路:对于一个二叉树的后序遍历序列来说,最后一个数一定是根节点,然后前面的数中,从最开始到第一个大于根节点的数都是左子树中的数,而后面到倒数第二个数应该都是大于根节点的,是右子树,如果后面的数中有小于根节点的,那么说明这个序列不是二叉搜索树的后序遍历序列。

    public class JudgeHouxubainli {
      public static void main(String[] args) {
        int[] array = {4,8,6,12,16,14,10};
        boolean result = false;
        result = judge(array);
        System.out.println(result);
      }
      private static boolean judge(int[] array){
        if(array.length == 1){
          return true;
        }
        if(array == null || array.length == 0){
          return false;
        }
        int root = array[array.length - 1];
        int i = 0;
        for(; i < array.length-1; ++i){
          if(array[i]>= root){
            break;
          }
        }
        int j = i;
        for(; j< array.length-1; ++j){
          if(array[j] < root){
            return false;
          }
        }
        judge(Arrays.copyOfRange(array, 0, i));
        judge(Arrays.copyOfRange(array, i, array.length-1));
        return true;
      }

    }

  • 相关阅读:
    Hanoi塔问题
    《OD学hive》第六周20160731
    《OD学Hive》第六周20160730
    《OD学Sqoop》数据转换工具Sqoop
    《OD学hive》第五周0723
    《OD学hive》第四周0717
    《OD学hadoop》第四周0716
    hadoop环境搭建笔记
    《OD学hadoop》第三周0710
    《OD学hadoop》第三周0709
  • 原文地址:https://www.cnblogs.com/huaiyinxiaojiang/p/6617148.html
Copyright © 2011-2022 走看看