用于生成BST(我的函数将其作为输入)的数组:[38,95,70,5,10,148,93]
到目前为止,该函数只返回[5,10]
,而不是按排序顺序返回所有元素.这种做法有何不妥?
function sortedArrayFromBST(tree,node,outputArray){
outputArray=outputArray||[]
// console.log(tree)
node=node||tree.root
console.log(node)
let left=node.left
console.log(left)
let right=node.right
console.log(right)
if(left ==null){
outputArray.push(node.data)
}else{
return sortedArrayFromBST(tree,left, outputArray)
}
if(right==null){
return outputArray
} else{
return sortedArrayFromBST(tree, right, outputArray)
}
}