override toString() function for TreeNode to output OJ's Binary Tree Serialization
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的更多相关文章
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
- 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...
- 【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 ...
- 【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 ...
- 【LEETCODE OJ】Binary Tree Preorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 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 ...
- LeetCode OJ 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 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 ...
随机推荐
- 爬虫-设置代理ip
1.为什么要设置代理ip 在学习Python爬虫的时候,经常会遇见所要爬取的网站采取了反爬取技术导致爬取失败.高强度.高效率地爬取网页信息常常会给网站服务器带来巨大压力,所以同一个IP反复爬取同一个网 ...
- 从零开始自学 Java Web
目录: 1.Java JDK下载安装及配置 2.eclipse下载与安装并测试 3.eclipse快捷键 4.Tomcat 下载与安装 5.Tomcat部署Web应用 6.Eclipse中配置Tomc ...
- ASP.NET MVC Bundles 之学习笔记
在网页中,我们经常需要引用大量的javascript和css文件,在加上许多javascript库都包含debug版和经过压缩的release版(比如jquery),不仅麻烦还很容易引起混乱,所以AS ...
- 我所理解的selenium之PO设计模式
下午,花了点时间来整理UI自动化设计,就把我所理解的PO设计模式项目结构脑图整理如下,有不对的地方还望多多包涵.谢谢
- Tomcat性能优化方案
1. 提高JVM栈内存Increase JVM heap memory 你使用过tomcat的话,简单的说就是"内存溢出". 通常情况下,这种问题出现在实际的生产环境中.产生这种问 ...
- UnityEditor扩展-右键拷贝资源路径到系统剪贴板
要点速记 命令添加到右键菜单 [MenuItem("Assets/Copy Asset Path")] 或 [MenuItem("Assets/Copy Asset Pa ...
- log4cpp简单使用及踩到的坑
log4cpp是log4j的一个扩展, C++开发者可用该库记录日志,可输出到终端,亦可保存到文件. 下面简单demo展示如何输出日志到输出终端. #include <iostream> ...
- jmeter功能按钮介绍
Jmeter本来是做性能测试的,所以有很多监听器都是针对性能的,随后边的不断发展,可以应用到接口自动化等测试工作上. 面板的文件菜单中一般都是打开.保存.新建测试的,如下图: 保存时可以直接ctrl+ ...
- 啥是MD5?
啥是MD5加密呢?为啥要使用MD5这种非对称的加密方式呢? 本文将通过漫画的形式来通俗易懂的讲述什么是MD5加密算法(Message Digest Algorithm MD5(中文名为消息摘要算法第五 ...
- python装饰器(披着羊皮的狼)
python装饰器的作用是在不改变原有函数的基础上,对函数的功能进行增加或者修改. 装饰器语法是python语言更加优美且避免很多繁琐的事情,flask中配置路由的方式便是装饰器. 首先python中 ...