Javascript n叉树InOrder函数
Javascript n-ary tree InOrder Function
我构建了一个函数来从二叉树 (InOrder) 中获取值。现在我想修改该函数,使其适用于 n 叉树。非常感谢您的帮助。
function stringFromInOrder(tree, position) {
if (!tree) {
return "";
}else if(tree.value === ""){
return false;
}
return stringFromInOrder(tree.left) + tree.value + stringFromInOrder(tree.right) ;
}
如果你想坚持递归:
function stringFromInOrder(tree, position) {
if (!tree) {
return "";
}else if(tree.value === ""){
return false;
}
var s = 0;
var i = 0
while (i < tree.children.count)
{
s += stringFromInOrder(tree.children[i]);
i++;
}
return tree.value + s;
}
我构建了一个函数来从二叉树 (InOrder) 中获取值。现在我想修改该函数,使其适用于 n 叉树。非常感谢您的帮助。
function stringFromInOrder(tree, position) {
if (!tree) {
return "";
}else if(tree.value === ""){
return false;
}
return stringFromInOrder(tree.left) + tree.value + stringFromInOrder(tree.right) ;
}
如果你想坚持递归:
function stringFromInOrder(tree, position) {
if (!tree) {
return "";
}else if(tree.value === ""){
return false;
}
var s = 0;
var i = 0
while (i < tree.children.count)
{
s += stringFromInOrder(tree.children[i]);
i++;
}
return tree.value + s;
}