Skip to content

DFS in TREE

Rayhan Uddin edited this page Jun 2, 2017 · 2 revisions
     Tree.prototype.traverse = function (callback) {
       var stack=[this];
       var n;

       while(stack.length>0) {

       n = stack.pop();
       callback(n.value);

      if (!n.children) {
        continue;
       }

     for (var i = n.children.length-1; i>=0; i--) {
       stack.push(n.children[i]);
        }
     }
    };
Clone this wiki locally