Chinaunix首页 | 论坛 | 博客
  • 博客访问: 320279
  • 博文数量: 15
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 179
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-09 18:16
文章分类

全部博文(15)

文章存档

2019年(1)

2018年(1)

2015年(7)

2013年(6)

我的朋友

分类: Java

2019-08-10 17:21:33

java实现

点击(此处)折叠或打开

  1. import java.io.*;
  2. import java.util.*;

  3. public class Main {

  4.     static final int MAXNODE = 1024;
  5.     
  6.     public static class btNode {
  7.         char value;
  8.         btNode left;
  9.         btNode right;
  10.     }
  11.     
  12.     static Stack<btNode> s = new Stack<btNode>();
  13.     static Queue<btNode> q = new LinkedList<btNode>();
  14.     static int index = 0;
  15.     static int max;
  16.     
  17.     static btNode buildTree(String nodeList[]) {
  18.         if (index > max - 1) {
  19.             return null;
  20.         }
  21.         char value = nodeList[index].charAt(0);
  22.         int flag = Character.getNumericValue(nodeList[index].charAt(1));
  23.         index++;
  24.         
  25.         if (value == '$') {
  26.             return null;
  27.         }

  28.         btNode tmpNode = new btNode();
  29.         tmpNode.value = value;
  30.         
  31.         if (flag == 0) {
  32.             tmpNode.left = buildTree(nodeList);
  33.             tmpNode.right = buildTree(nodeList);
  34.         }
  35.         
  36.         return tmpNode;
  37.     }
  38.     
  39.     static void printTree(btNode root) {
  40.         while(root != null) {
  41.             s.push(root);
  42.             root = root.right;
  43.         }
  44.         
  45.         while (!s.empty()) {
  46.             System.out.print(s.peek().value + " ");
  47.             q.add(s.pop());
  48.         }
  49.         
  50.         while (!q.isEmpty()) {
  51.             btNode tmp = q.poll();
  52.             
  53.             if (tmp.left != null) {
  54.                 printTree(tmp.left);
  55.             }
  56.             
  57.         }
  58.     }
  59.     public static void main(String[] args) throws IOException {
  60.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  61. // String str = "a0 b0 $1 c0 d0 $1 e1 f1 $1";
  62. // max = 9;
  63.         max = Integer.parseInt(br.readLine());
  64.         String str = br.readLine();
  65.         String nodeList[] = str.split(" ");
  66.         btNode root = buildTree(nodeList);
  67.         printTree(root);
  68.     }

  69. }


阅读(269482) | 评论(0) | 转发(0) |
0

上一篇:k8s容器IO限速

下一篇:没有了

给主人留下些什么吧!~~