Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

tree travers


const depthFirstTraversal = (root) => {
  if (root === null) return [];

  const result = [];
  const stack = [root];

  while (stack.length > 0) {
    const current = stack.pop();
    result.push(current.data);
    if (current.right) stack.push(current.right);
    if (current.left) stack.push(current.left);
  }
  return result;
};
 
PREVIOUS NEXT
Tagged: #tree #travers
ADD COMMENT
Topic
Name
2+2 =