public class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
}
import java.util.LinkedList;
import java.util.Queue; public class Wrapper {
public static String treeNodeToString(TreeNode root) {
if (root == null) {
return "[]";
} String output = "";
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.add(root);
while (!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.remove(); if (node == null) {
output += "null, ";
continue;
} output += String.valueOf(node.val) + ", ";
nodeQueue.add(node.left);
nodeQueue.add(node.right);
}
return "[" + output.substring(0, output.length() - 2) + "]";
} public static TreeNode stringToTreeNode(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return null;
} String[] parts = input.split(",");
String item = parts[0];
TreeNode root = new TreeNode(Integer.parseInt(item));
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.add(root); int index = 1;
while (!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.remove(); if (index == parts.length) {
break;
} item = parts[index++];
item = item.trim();
if (!item.equals("null")) {
int leftNumber = Integer.parseInt(item);
node.left = new TreeNode(leftNumber);
nodeQueue.add(node.left);
} if (index == parts.length) {
break;
} item = parts[index++];
item = item.trim();
if (!item.equals("null")) {
int rightNumber = Integer.parseInt(item);
node.right = new TreeNode(rightNumber);
nodeQueue.add(node.right);
}
}
return root;
} public static void prettyPrintTree( TreeNode node,
String prefix,
boolean isLeft) {
if (node == null) {
System.out.println("Empty tree");
return;
} if (node.right != null) {
prettyPrintTree(node.right, prefix + (isLeft ? "│ " : " "),
false);
} System.out.println(prefix + (isLeft ? "└── " : "┌── ") + node.val); if (node.left != null) {
prettyPrintTree(node.left, prefix + (isLeft ? " " : "│ "), true);
}
} public static void prettyPrintTree(TreeNode node) {
prettyPrintTree(node, "", true);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; public class MainClass {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
TreeNode root = Wrapper.stringToTreeNode(line);
Wrapper.prettyPrintTree(root);
}
}
}

最新文章

  1. MySQL复制环境(主从/主主)部署总结性梳理
  2. [ActionScript 3.0] 通过内联函数对addFrameScript方法传递参数
  3. C语言PIC16 serial bootloader和C#语言bootloader PC端串口通信程序
  4. datetimepicker一个不错的日历android特效
  5. swift中文文档- 类型转换
  6. SAP 用事务码SQVI 做简单报表 .
  7. CSS常用样式整理
  8. Hadoop SecondaryNameNode备份及恢复
  9. Pop Sequence (栈)
  10. ### 学习《C++ Primer》- 7
  11. 再回首,Java温故知新(九):Java基础之流程控制语句
  12. java 线程三种实现方式
  13. Wpf解决TextBox文件拖入问题、拖放问题
  14. 痛苦的版本对齐(3) cygwin下的路径引用
  15. C#版-百度网盘API的实现(二)
  16. Linux Django项目部署
  17. gdb remote 使用
  18. java 使用jdbc连接Greenplum数据库和Postgresql数据库
  19. html 刷新重载方法汇总
  20. P1862输油管道问题

热门文章

  1. pycharm中使用2to3
  2. 一篇JavaScript技术栈带你了解继承和原型链
  3. npm link 的用法
  4. oracle 如何通过分组计数查出重复数据?
  5. mysql中的正则操作 匹配手机号,匹配中文,替换
  6. android双进程守护,让程序崩溃后一定可以重启
  7. Android如何屏蔽home键和recent键
  8. sigmoid与softmax 二分类、多分类的使用
  9. SpringBoot项目从Git拉取代码并完成编译打包启动的sh自动脚本
  10. asp.net core mvc 里的application中的start,end等事件