class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; } @Override
public String toString(){
if(this == null) return "";
StringBuilder sb = new StringBuilder();
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(this);
sb.append(val + ",");
while(!queue.isEmpty()){
TreeNode tmp = queue.poll();
sb.append((tmp.left == null ? "#" : tmp.left.val) + ",");
sb.append((tmp.right == null ? "#" : tmp.right.val) + ",");
if(tmp.left != null) queue.add(tmp.left);
if(tmp.right != null) queue.add(tmp.right);
}
return sb.toString(); }
}

override toString() function for TreeNode to output OJ's Binary Tree Serialization的更多相关文章

  1. 【LeetCode OJ】Binary Tree Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...

  2. 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...

  3. 【LeetCode OJ】Binary Tree Level Order Traversal II

    Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...

  4. 【LeetCode OJ】Binary Tree Maximum Path Sum

    Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...

  5. 【LEETCODE OJ】Binary Tree Preorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...

  6. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  7. LeetCode OJ 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  8. LeetCode OJ 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  9. LeetCode OJ 199. Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

随机推荐

  1. Oracle GUID转换为String

    Oracle中guid属于Raw(16)类型, 查询的时候如果不使用下面的函数, 程序中得到的是数组(byte[]). 在extjs环境下, 会带来数组的反序列化问题(newtonsoft.json) ...

  2. 通过IDEA解决spring配置文件

    来自:https://blog.csdn.net/yanghanxiu/article/details/79366263 (其实尼可以使用springboot 这样就不用配置一大堆东西了喵!) 每次创 ...

  3. pytest使用笔记(二)——pytest+allure配置使用

    使用环境及预置条件 开发工具:pycharm 操作系统:win10 开发语言:python3.6 使用库:pytest4.0,pytest-allure-adaptor 注意不要安装allure-py ...

  4. CentOS搭建Docker Hub私有仓库

    docker pull registry拉取registry镜像 docker images查看镜像 docker run -d -p 5000:5000 -v /opt/data/registry: ...

  5. python魔法方法大全

    1.python魔法方法详解: python魔法方法是可以修改重载的,如果你的对象实现(重载)了这些方法中的某一个,那么这个方法就会在特殊的情况下被 Python 所调用,你可以定义自己想要的行为,而 ...

  6. 从零系列--node爬虫利用进程池写数据

    1.主进程 const http = require('http'); const fs = require('fs'); const cheerio = require('cheerio'); co ...

  7. k8s学习-资源管理

    在云计算领域,资源可被分为计算资源.网络资源.存储资源三大类,也可被分别称作为计算云.网络云.存储云.在以容器为核心的云平台上,应用容器镜像也是一种资源. 一.计算资源管理 计算资源在云平台上主要指应 ...

  8. zookeeper客户端相关命令

    windows环境:    本机 直接 点机zkcli.cmd linux环境: 连接到zookeeper server ./zkCli.sh -server localhost:2181 help命 ...

  9. Sqlserver 每日订单半小时数据统计

    ) '订单数' FROM (SELECT CASE THEN ), create_at, ) ),DATEPART(hh, create_at))+':00:00') ELSE ), create_a ...

  10. 学习记录 div悬停在顶部 。div阻止冒泡

    如何让一个div可点击,并且div里面的a元素也能点击? 楼主应该是想要这样的,阻止事件冒泡 点击里面的a的时候不触发外面的div的点击事件 <script type="text/ja ...