-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
paramag
authored and
paramag
committed
Sep 27, 2018
1 parent
23cf380
commit 35df6d1
Showing
24 changed files
with
1,061 additions
and
26 deletions.
There are no files selected for viewing
Binary file not shown.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DataStructures.Libraries.Trees | ||
{ | ||
[TestClass] | ||
public class RootToLeafSum | ||
{ | ||
public bool FindRootToLeafSumPath(BinaryTreeNode root, int sum, List<int> result) | ||
{ | ||
// check if it's leaf node | ||
if (root.Left == null && root.Right == null) | ||
{ | ||
bool isTarget = false; | ||
if (sum == root.Value) | ||
{ | ||
isTarget = true; | ||
result.Add(root.Value); | ||
} | ||
|
||
return isTarget; | ||
} | ||
|
||
bool isTargetSumLeft = this.FindRootToLeafSumPath(root.Left, sum - root.Value, result); | ||
bool isTargetSumRight = this.FindRootToLeafSumPath(root.Right, sum - root.Value, result); | ||
|
||
if (isTargetSumLeft) result.Add(root.Value); | ||
if (isTargetSumRight) result.Add(root.Value); | ||
|
||
return isTargetSumLeft || isTargetSumRight; | ||
} | ||
|
||
[TestMethod] | ||
public void TestFindRootToLeaftSumPath() | ||
{ | ||
/// 20 | ||
/// / \ | ||
/// 10 30 | ||
/// / \ / \ | ||
/// 5 12 25 35 | ||
|
||
var root = new BinaryTreeNode(20); | ||
|
||
root.Left = new BinaryTreeNode(10); | ||
root.Right = new BinaryTreeNode(30); | ||
|
||
root.Left.Left = new BinaryTreeNode(5); | ||
root.Left.Right = new BinaryTreeNode(12); | ||
|
||
root.Right.Left = new BinaryTreeNode(25); | ||
root.Right.Right = new BinaryTreeNode(35); | ||
|
||
List<int> listArray = new List<int>(); | ||
|
||
bool isTargetExist = this.FindRootToLeafSumPath(root, 80, listArray); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using DataStructures.Libraries.Trees; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Algorithms.Problem.BinaryTree | ||
{ | ||
[TestClass] | ||
public class SerializeAndDeserialize | ||
{ | ||
public BinaryTreeNode DeSerialize(int[] serializedString, int count) | ||
{ | ||
// this is leaf node | ||
if (serializedString[count] == -1) | ||
{ | ||
return null; | ||
} | ||
|
||
int value = serializedString[count]; | ||
BinaryTreeNode treeNode = new BinaryTreeNode(value); | ||
|
||
|
||
treeNode.Left = this.DeSerialize(serializedString, count + 1); | ||
treeNode.Right = this.DeSerialize(serializedString, count + 1); | ||
|
||
return treeNode; | ||
} | ||
|
||
public string Serialize(BinaryTreeNode root, string str) | ||
{ | ||
if (root != null) | ||
{ | ||
str += root.Value + ","; | ||
|
||
if (root.Left == null && root.Right == null) | ||
{ | ||
str = str + "-1,"; | ||
System.Diagnostics.Debug.WriteLine(str); | ||
|
||
return str; | ||
} | ||
} | ||
|
||
if (root != null) str = Serialize(root.Left, str); | ||
if (root!= null) str = Serialize(root.Right, str); | ||
|
||
return str; | ||
} | ||
|
||
[TestMethod] | ||
public void TestSerialize() | ||
{ | ||
var binarySearchTree = new BinarySearchTree(); | ||
binarySearchTree.PopulateDefaultBalanceTree(); | ||
|
||
//BinaryTreeNode node1 = new BinaryTreeNode(10); | ||
//node1.Left = new BinaryTreeNode(5); | ||
//node1.Right = new BinaryTreeNode(12); | ||
|
||
string str = this.Serialize(binarySearchTree.Root, ""); | ||
string serializedString = str.TrimEnd(','); | ||
|
||
string[] serializedStringArray = serializedString.Split(','); | ||
List<int> serializedIntArray = new List<int>(); | ||
|
||
foreach(string ser in serializedStringArray) | ||
{ | ||
serializedIntArray.Add(int.Parse(ser)); | ||
} | ||
|
||
BinaryTreeNode node = this.DeSerialize(serializedIntArray.ToArray(), 0); | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LeetCode.Arrays | ||
{ | ||
[TestClass] | ||
public class ArrayManipulation | ||
{ | ||
public int[] MoveZerosToRight2(int[] array) | ||
{ | ||
int i = 0; | ||
int j = 0; | ||
|
||
while (i < array.Length) | ||
{ | ||
if (array[i] != 0) | ||
{ | ||
if (i != j) | ||
array[j] = array[i]; | ||
|
||
j++; | ||
} | ||
i++; | ||
} | ||
|
||
while (j < array.Length) | ||
{ | ||
array[j] = 0; | ||
j++; | ||
} | ||
|
||
return array; | ||
} | ||
|
||
public int[] MoveZerosToRight(int[] array) | ||
{ | ||
for(int i=0; i< array.Length; i++) | ||
{ | ||
if (array[i] == 0) | ||
{ | ||
int j = i + 1; | ||
while (j < array.Length && array[j] == 0) | ||
{ | ||
j++; | ||
} | ||
|
||
if (j >= array.Length) break; | ||
|
||
// swap the current array | ||
int temp = array[i]; | ||
array[i] = array[j]; | ||
array[j] = temp; | ||
} | ||
} | ||
|
||
return array; | ||
} | ||
|
||
[TestMethod] | ||
public void TestMoveZerosToRight() | ||
{ | ||
int[] resultArray = this.MoveZerosToRight(new[] { 0, 1, 2, 0, 3, 0 }); | ||
resultArray = this.MoveZerosToRight2(new[] { 0, 1,2,0,3,0}); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LeetCode.Arrays | ||
{ | ||
public class KMostFrequentElement | ||
{ | ||
public int[] GetFrequentElements(int[] array, int k) | ||
{ | ||
return null; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LeetCode.Arrays | ||
{ | ||
[TestClass] | ||
public class MergeTwoSortedArray | ||
{ | ||
public void MergeTwoSortedArrayHelper(int[] arrayA, int[] arrayB) | ||
{ | ||
int m = arrayA.Length - 1; | ||
int n = arrayB.Length - 1; | ||
|
||
int counterMidA = arrayA.Length - arrayB.Length - 1; | ||
int counterEndA = m; | ||
int counterB = n; | ||
|
||
while (counterB >= 0 && counterMidA >= 0) | ||
{ | ||
if (arrayA[counterMidA] > arrayB[counterB]) | ||
{ | ||
arrayA[counterEndA] = arrayA[counterMidA]; | ||
counterMidA--; | ||
} | ||
else | ||
{ | ||
arrayA[counterEndA] = arrayB[counterB]; | ||
counterB--; | ||
} | ||
|
||
counterEndA--; | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void TestMergeTwoSortefArray() | ||
{ | ||
int[] arrayA = new int[] { 1, 5, 9, 10, 15, 20, 0, 0, 0, 0 }; | ||
int[] arrayB = new int[] { 2, 3, 8, 13}; | ||
|
||
this.MergeTwoSortedArrayHelper(arrayA, arrayB); | ||
} | ||
} | ||
} |
Oops, something went wrong.