1028. 从先序遍历还原二叉树
一般来说仅 中序遍历 是无法还原二叉树的,但是这个字符串里带有深度信息。
所以可以先用正则表达式把值及其深度提取出来,然后重建二叉树。
例如当前节点深度为i,那么下下个深度为i+1的节点就是右字数的首节点。
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func recoverFromPreorder(S string) *TreeNode {
nodeReg,_:=regexp.Compile("[-]*[0-9]+")
nodes:=nodeReg.FindAllStringSubmatch(S,-1)
numReg,_:=regexp.