您的位置:首页 > 其它

LeetCode 297. Serialize and Deserialize Binary Tree

2016-04-04 17:47 483 查看

题目

LeetCode 297. Serialize and Deserialize Binary Tree

仿照

331. Verify Preorder Serialization of a Binary Tree

做的,将叶节点和非叶节点分开考虑,先序遍历二叉树,因为节点node有可能有多位或者负数,所以需要用逗号分隔:

序列化

如1,2,#,#,#

表示叶子节点,先序遍历

反序列化

也是用栈实现的,非递归,需要记录当前向右或者向左状态

源码

package com.leetcode;

import java.util.Stack;

public class Leetcode297 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;

TreeNode(int x) {
val = x;
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Leetcode297 lc = new Leetcode297();
TreeNode root = lc.new TreeNode(1);
root.left = lc.new TreeNode(2);
//        root.right = lc.new TreeNode(3);
//        root.left.left = lc.new TreeNode(4);
//        root.left.right = lc.new TreeNode(5);
//        root.right.left = lc.new TreeNode(6);
//        root.right.right = lc.new TreeNode(7);
String data = lc.serialize(root);
System.out.println(data);
TreeNode reroot = lc.deserialize(data);
lc.firstRoot(reroot, 0);
}

// Encodes a tree to a single string.
public String serialize(TreeNode root) {
return firstRoot(root);
}

// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
if (data.equals("")) return null;
String [] fs = data.split(",");
Stack<TreeNode> space = new Stack<TreeNode>();
int state = 0;
TreeNode root = null;
for (int i = 0; i < fs.length; i++) {
TreeNode node = null;
if (state == 0) {
if (!fs[i].equals("#")) {
node = new TreeNode(Integer.parseInt(fs[i]));
if (!space.isEmpty()) {
TreeNode lastNode = space.peek();
lastNode.left = node;
}
space.push(node);
} else {
state = 1;
}
} else if (state == 1) {
if (!fs[i].equals("#")) {
node = new TreeNode(Integer.parseInt(fs[i]));
if (!space.isEmpty()) {
TreeNode lastNode = space.peek();
lastNode.right = node;
}
state = 0;
space.push(node);
} else {
TreeNode lastNode = space.pop();
while (!space.isEmpty() && space.peek().right == lastNode) {
lastNode = space.pop();
}
}
}
if (root == null) root = node;
}
return root;
}

public String firstRoot(TreeNode root) {
if (root == null) return "";
StringBuffer sb = new StringBuffer("");
Stack<TreeNode> space = new Stack<TreeNode>();
space.push(root);
while (!space.isEmpty()) {
TreeNode node = space.pop();
if (node.val != Integer.MIN_VALUE) {
sb.append(sb.length() == 0 ? node.val : "," + node.val);
if (node.right != null) {
space.push(node.right);
} else {
space.push(new TreeNode(Integer.MIN_VALUE));
}

if (node.left != null) {
space.push(node.left);
} else {
space.push(new TreeNode(Integer.MIN_VALUE));
}
} else {
sb.append(sb.length() == 0 ? "#" : ",#");
}
}
return sb.toString();
}

public void firstRoot(TreeNode root, int sign) {
if (root == null) return;
Stack<TreeNode> space = new Stack<TreeNode>();
space.push(root);
while (!space.isEmpty()) {
TreeNode node = space.pop();
System.out.println(node.val);
if (node.right != null) space.push(node.right);
if (node.left != null) space.push(node.left);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: