Skip to content

Commit

Permalink
Merge pull request #172 from TiaLow/master
Browse files Browse the repository at this point in the history
Function to determine if a linked list is a palindrome
  • Loading branch information
sat5297 authored Oct 9, 2020
2 parents 2ab27d6 + a3cc859 commit 7223a70
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
45 changes: 45 additions & 0 deletions leavesBinaryTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

// JavaScript implementation of a function that finds the number of leaves in a binary tree
// function's signature is a binary tree as a parameter, returns the number of leaves in the tree
// relies on the assumption that the input is a binary tree with a root, and made up of nodes with left and right properties on them, as illustrated below
// function uses preOrder depth traversal method

class Node {

constructor(value, left = null, right = null) {
this.value = value;
this.left = left;
this.right = right;
}

}

class BinaryTree {

constructor( root = null) {
this.root = root;
}

}

function numLeavesInTree(binaryTree) {

let numLeaves = 0;

function _preOrderTraversal(root) {

if(!root) {
return;
}

numLeaves += 1;
_preOrderTraversal(root.left);
_preOrderTraversal(root.right);

}

_preOrderTraversal(binaryTree.root);
return numLeaves;

}
59 changes: 59 additions & 0 deletions listPalindrome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

// Javascript implementation of a stand alone function that determines if a linked list is a palindrome
// function's signature is a linked list as a parameter and returns true if it's a palindrome, false if not
// relies on a linked list node implementation as written below

class Node {

constructor(value, next = null) {
this.value = value;
this.next = next;
}
}


class LinkedList {

constructor() {
this.head = null;
}

insert(value) {
this.head = new Node(value, this.head);
}

}

function determineListPalindrome(linkedList) {

let arr =[];

let currentNode = linkedList.head;

if(currentNode) {
arr.push(currentNode.value);
}

while(currentNode.next) {
currentNode = currentNode.next;
arr.push(currentNode.value);
}

for(let i = 0; i < arr.length/2; i++) {

if(arr[i] !== arr[arr.length - ( i + 1 ) ] ){

return false;

}

return true;

}


}



0 comments on commit 7223a70

Please sign in to comment.