是差点运气,可我一直在努力!
当前进程:
- 开始时间:2020.6.27
- 结束时间:undefined
GitHub仓库:https://github.com/Cundefined/JavaScript-or-TypeScript-for-LeetCode
1、题目要求
2、解题思路
方法:广度优先搜索(层序遍历)
序列化:
1、标准的层序遍历模板,维持一个队列
2、返回指定的字符串形式的结果
反序列化:
也是层序遍历的思想,维持一个队列
1、由序列化的结果可知当前元素的父节点肯定在它前面
2、因此,需要提前判断当前元素是其前面节点的左孩子还是有孩子
3、这样,才能方便的连接节点
2.1、Java Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Codec {
// Encodes a tree to a single string.
public String serialize(TreeNode root) {
StringBuilder res = new StringBuilder("[");
Queue<TreeNode> queue = new LinkedList();
queue.add(root);
while(