2016-10-22 7 views
0

バイナリツリー(InOrder)から値を取得する関数を作成しました。今、私はそれがn-aryツリーで動作するようにその関数を変更したいと思います。私はあなたの助けに感謝します。Javascript n-tree InOrder関数

function stringFromInOrder(tree, position) { 
    if (!tree) { 
     return ""; 
    }else if(tree.value === ""){ 
      return false; 
     } 
    return stringFromInOrder(tree.left) + tree.value + stringFromInOrder(tree.right) ; 
} 
+0

どのようにこの場合のツリーを実装して順番 –

答えて

0

あなたは再帰に固執する場合:

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; 
} 
+0

でのn進ツリーを横断する標準的な方法はありませんか? n-aryツリーを作成しますか?あなたの答えをありがとう。 –