A friend came across an interview as follows:
Output the [[” F “, “C”, “A”], [” F “, “C”, “D”], [” F “, “C”, “D”, “B”], [” F “, “E”, “H”], [” F “, “E”, “G”], [” F “, “E”, “G”, “M”]] format array, ask how to do? Field coding
Achieved two versions, the second version of the fish find time to manage the idea, simple.
/ / define data format let nodes = {val: "F", l: {val: 'C', l: {val: 'A'}, r: {val: 'D', l: {val: 'B'}}}, r: {val: 'E' l: {val: 'H'}, r: {val: 'G', l: {val: 'M'}}}} console.log(nodes) // Handle data let res = [] function f(node) {if(! Node.val) {console.log(' Data structure exception! ') return false; } if(! node.l || ! node.r) { res.push([...(node.arr || []), node.val]) } if(node.l) { f(Object.assign(node.l,{ arr: [...(node.arr || []), node.val] })) } if(node.r) { f(Object.assign(node.r,{ arr: [... (node. Arr | | []), the node. The val]}}})) / / execution f (nodes) / / output to the console. The log (JSON. Stringify (res))Copy the code
Tried the next normal input, do not know if there is a better solution, ask big guy to correct.