Skip to content

Commit

Permalink
Use tree helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Fran Varney committed Mar 31, 2016
1 parent 4af5c5e commit 25a86cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
21 changes: 5 additions & 16 deletions solutions/leetcode/144-binary-tree-preorder-traversal/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const Assert = require('assert');
const Helpers = require('../../../helpers');

function preorderTraversal(root) {
var items = [];

function preorder(node) {
if (node) {
items.push(node.val);
items.push(node.value);
preorder(node.left);
preorder(node.right);
}
Expand All @@ -16,21 +17,9 @@ function preorderTraversal(root) {
return preorder(root);
};

var tree = {
val: 1,
left: {
val: 5
},
right: {
val: 2,
left: {
val: 3
}
}
}

var results = preorderTraversal(tree);
Assert.deepEqual(results, [1,5,2,3]);
var tree = Helpers.initTree([6,2,3,9,1,7]);
var results = preorderTraversal(tree.root);
Assert.deepEqual(results, [6,2,1,3,9,7]);

console.log(results);

Expand Down
21 changes: 5 additions & 16 deletions solutions/leetcode/94-binary-tree-inorder-traversal/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const Assert = require('assert');
const Helpers = require('../../../helpers');

function inorderTraversal(root) {
var items = [];

function inorder(node) {
if (node) {
inorder(node.left);
items.push(node.val);
items.push(node.value);
inorder(node.right);
}

Expand All @@ -16,21 +17,9 @@ function inorderTraversal(root) {
return inorder(root);
};

var tree = {
val: 1,
left: {
val: 5
},
right: {
val: 2,
left: {
val: 3
}
}
}

var results = inorderTraversal(tree);
Assert.deepEqual(results, [5,1,3,2]);
var tree = Helpers.initTree([6,2,3,9,1,7]);
var results = inorderTraversal(tree.root);
Assert.deepEqual(results, [1,2,3,6,7,9]);

console.log(results);

Expand Down

0 comments on commit 25a86cc

Please sign in to comment.