Created
March 31, 2019 01:43
-
-
Save dineshbalaji/80183b9b3df03fea428f6928f3c6728b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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