Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="alert alert-note">
  <h3>Binary Tree Traversal</h3>
  <p>
    The process of visiting each node in the tree exactly once in some order. Visiting means reading/processing data in a node.</p>

  <li>
    <strong>Breadth-First</strong> is also called as <strong>Level-Order</strong> traversal. We visit all of it's children before visiting it's grand children.

    <img src="https://i.imgur.com/U7XLxMX.png" alt="Breadth-First">
  </li>
  <li>
    <strong> Depth-First </strong> we visit all the grand children before going to the siblings. The relative order of visiting left and right subtree can be different like <mark>in-order, pre-order and post-order.</mark> Visiting a child is visiting the complete subtree in that path.

    <img src="https://i.imgur.com/tMyc0BT.png" alt="">
  </li>
  <br>
  <img src="https://i.imgur.com/DYkRBqz.png">
</div>
              
            
!

CSS

              
                .alert {
  padding: 20px;
  border-radius: 25px;
  margin-bottom: 15px;
}

.alert-tip {
  background-color: #def6dd;
}

.alert-note {
  background-color: #efd9fd;
  overflow-x: auto;
}
pre {
  overflow-x: auto;
  white-space: pre-wrap;
  white-space: -moz-pre-wrap;
  white-space: -pre-wrap;
  white-space: -o-pre-wrap;
  word-wrap: break-word;
}

              
            
!

JS

              
                /* 
Nodes are created in the heap seciton of the memory 
Address of the root node we keep for a tree.
*/

class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

/*
O(n) time | O(n) space because we have queue.
Level-Order Traversal
*/
function breadthFirst(root) {
  // Write your code here
  if (root == null) return;
  let queue = [root]; // O(n) space
  while (queue.length > 0) {
    let current = queue.shift(); // removing element at front.
    visit(current);
    if (current.left) queue.push(current.left);
    if (current.right) queue.push(current.right);
  }
}

let result = [];

function visit(node) {
  console.log(node.data);
  result.push(node.data);
}

// unit tests
// do not modify the below code
describe("Breadth First Traversal", () => {
  let root = null;

  beforeEach(() => {
    root = null;
    root = insert(root, 15);
    root = insert(root, 10);
    root = insert(root, 20);
    root = insert(root, 25);
    root = insert(root, 8);
    root = insert(root, 12);
  });
  it("should work correctly", () => {
    breadthFirst(root);
    expect(result).toEqual([15, 10, 20, 8, 12, 25]);
  });
});

function insert(root, data) {
  // If the tree is empty create new node.
  let notToInsert = new Node(data);

  let current = root;
  let previous = null;
  while (current != null) {
    previous = current;
    if (data <= current.data) current = current.left;
    else current = current.right;
  }

  if (previous == null) return notToInsert;
  else if (data <= previous.data) previous.left = notToInsert;
  else previous.right = notToInsert;

  return root;
}

              
            
!
999px

Console