normalize's recent timeline updates
normalize

normalize

V2EX member #437544, joined on 2019-08-23 10:06:23 +08:00
normalize's recent replies
Sep 7, 2019
Replied to a topic by fenghuang 程序员 问大家一个面试题
//12(2(34,4(,5)),6(7,))
//没有测试太多
function treeNode(val) {
this.val = parseInt(val)
this.right = null
this.left = null
}

function stringToTree(str) {
var ary = [] //[1,2,3,4,null,5,6,7]
var re = /\d+/g
re.lastindex = 0
var match

for(var i = 0; i < str.length; i++) {
if(str[i] == ',' && str[i - 1] == '('){
ary.push(null)
}else if(str[i] == ')') {
if(str[i - 1] == ',') {
ary.push(null)
}

var preNode = ary.slice(-3)
ary = ary.slice(0, -3)

preNode[0].left = preNode[1]
preNode[0].right = preNode[2]
ary.push(preNode[0])

}else if(str[i] == '('){
continue
}else if(match = re.exec(str)) {
ary.push(new treeNode(match[0]))
i = match.index + match[0].length - 1
}
}

return ary[0]
}
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2367 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 16ms · UTC 05:48 · PVG 13:48 · LAX 22:48 · JFK 01:48
♥ Do have faith in what you're doing.