Skip to content

Instantly share code, notes, and snippets.

@dineshbalaji
Created March 31, 2019 01:43
Show Gist options
  • Save dineshbalaji/80183b9b3df03fea428f6928f3c6728b to your computer and use it in GitHub Desktop.
Save dineshbalaji/80183b9b3df03fea428f6928f3c6728b to your computer and use it in GitHub Desktop.
/* Binary Tree design - deepthTraversal using loop - pre-order*/
function deepthTraversalUsingLoop(tree) {
let result = [];
let stackList = [tree];
while (stackList.length > 0) {
let node = stackList.pop();
result.push(node.value); // pre-order tracing
node.right && stackList.push(node.right);
node.left && stackList.push(node.left);
}
console.log(` result of deepthTravesal using "while loop" `, result);
}
deepthTraversalUsingLoop(tree);
/* Binary Tree design - deepthTraversal using loop - in order*/
function deepthTravesalInOrderUsingLoop(tree) {
let result = [];
let stackList = [];
let node = tree;
while (!node || stackList.length > 0) {
while (!node) {
stackList.push(node);
node = node.left;
}
node = stackList.pop();
result.push(node.value);
node = node.right;
}
}
deepthTravesalInOrderUsingLoop(tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment