zoukankan      html  css  js  c++  java
  • 数据结构-二叉树相关问题及解答【3】

    1,根据字符串输出一个【前,中,后,层】二叉排序树

      在某个存储介质以如下的形式保存一颗二叉树

    1(2(3,4(,5)),6(7,))

    观察后发现,每个节点的格式为

    X,X可以为空

    或者X(Y,Z),其中X不可以为空

    请输出上述二叉树的前、中、后、层遍历。

    package com.cnblogs.mufasa.demo1;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Queue;
    
    class Node{
        int l;
        int r;
        public Node(int l,int r){
            this.l=l;
            this.r=r;
        }
    }
    
    public class myTree {
        String str;
        String ans;
        Queue<Node> queue= new LinkedList<Node>();
        public myTree(String str){
            this.str=str;
        }
        public String normalOut(){
            ans="";
            int len=str.length();
            deal(0,len-1);
            return ans;
        }
    
        private void deal(int l,int r){
            if(l>r){
                return;
            }
            int cont=0,mid=-1;
            for(int i=l+2;i<r;i++){//示例:1(2(3,4(,5)),6(7,))
                if(str.charAt(i)=='(')
                    cont++;
    
                if(str.charAt(i)==','&& cont==0){
                    mid=i;
                    break;
                }
    
                if(str.charAt(i) == ')')
                    cont--;
            }
            if(mid!=-1){
                ans=ans+str.charAt(l);//前序遍历
                deal(l+2,mid-1);
    //            ans=ans+str.charAt(l);//中序遍历
                deal(mid+1,r-1);
    //            ans=ans+str.charAt(l);//后续遍历
            }else {
                ans=ans+str.charAt(l);
            }
        }
    
        public String leveOut(){
            ans="";
            int len=str.length();
            queue.add(new Node(0,str.length()-1));
            while (queue.size()!=0){
                deal1();
            }
            return ans;
        }
    
        private void deal1(){
            Node pre=queue.poll();
            int l=pre.l,r=pre.r;
            if(l>r){
                return;
            }
            int cont=0,mid=-1;
            for(int i=l+2;i<r;i++){//示例:1(2(3,4(,5)),6(7,))
                if(str.charAt(i)=='(')
                    cont++;
    
                if(str.charAt(i)==','&& cont==0){
                    mid=i;
                    break;
                }
    
                if(str.charAt(i) == ')')
                    cont--;
            }
            if(mid!=-1){
                ans=ans+str.charAt(l);//层级遍历
                queue.add(new Node(l+2,mid-1));
                queue.add(new Node(mid+1,r-1));
            }else {
                ans=ans+str.charAt(l);
            }
        }
    
    
        public static void main(String[] args){
            String str="1(2(3,4(,5)),6(7,))";
            myTree tree=new myTree(str);
            System.out.println(tree.normalOut());
            System.out.println(tree.leveOut());
        }
    }
    View Code

    2,其他问题待

  • 相关阅读:
    Codeforces 912 D. Fishes (贪心、bfs)
    Codeforces 908 D.New Year and Arbitrary Arrangement (概率&期望DP)
    HDU
    HDU
    POJ-2299 Ultra-QuickSort (树状数组)
    deque!
    HDU
    乘法逆元
    Codeforces 911D. Inversion Counting (数学、思维)
    Codeforces 909E. Coprocessor (拓扑、模拟)
  • 原文地址:https://www.cnblogs.com/Mufasa/p/11478464.html
Copyright © 2011-2022 走看看