Created
June 14, 2017 18:30
-
-
Save jianminchen/f6c6624f58cfef4e3266e71c54398834 to your computer and use it in GitHub Desktop.
Binary tree Root to Leaf Path Sum - Two with the same value - similar to Leetcode 112 path sum -
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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace RootToLeafPathSum_TwoWithSameValue | |
{ | |
/// <summary> | |
/// binary tree root to leaf path sum with two path same value | |
/// </summary> | |
class Program | |
{ | |
internal class Tree | |
{ | |
public Tree Left { get; set; } | |
public Tree Right { get; set; } | |
public int Value { get; set; } | |
public Tree(int v) | |
{ | |
Value = v; | |
Left = null; | |
Right = null; | |
} | |
} | |
static void Main(string[] args) | |
{ | |
testCase1(); | |
testCase2(); | |
testCase3(); | |
} | |
// should return true | |
public static void testCase1() | |
{ | |
Tree root = new Tree(3); | |
root.Left = new Tree(4); | |
root.Left.Left = new Tree(1); | |
root.Left.Right = new Tree(1); | |
Debug.Assert( CheckRootToLeafPathSumDuplicate(root)); | |
} | |
// should return false | |
public static void testCase2() | |
{ | |
Tree root = new Tree(1); | |
root.Left = new Tree(3); | |
root.Right = new Tree(4); | |
Debug.Assert( !CheckRootToLeafPathSumDuplicate(root)); | |
} | |
// should return true | |
public static void testCase3() | |
{ | |
Tree root = new Tree(1); | |
root.Left = new Tree(3); | |
root.Right = new Tree(4); | |
root.Left.Left = new Tree(5); | |
root.Left.Right = new Tree(1); | |
Debug.Assert( CheckRootToLeafPathSumDuplicate(root)); | |
} | |
/// <summary> | |
/// 1. http://stackoverflow.com/questions/2444033/get-dictionary-key-by-value | |
/// 2. https://msdn.microsoft.com/en-us/library/bb340482(v=vs.110).aspx | |
/// 3. http://stackoverflow.com/questions/15741674/how-to-find-key-value-pair-in-a-dictionary-with-values-0-with-key-matching-a-c | |
/// </summary> | |
/// <param name="root"></param> | |
/// <returns></returns> | |
public static bool CheckRootToLeafPathSumDuplicate(Tree root) | |
{ | |
if (root == null) | |
{ | |
return false; | |
} | |
var sumLookup = new Dictionary<int, int>(); | |
addRootToLeafPathSum(root, 0, sumLookup); | |
int count = sumLookup.Where( r => r.Value > 1).Count(); | |
if (count > 0) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/// <summary> | |
/// depth first search using recursive function | |
/// base case - reach leaf node, add sum to the dictionary | |
/// </summary> | |
/// <param name="root"></param> | |
/// <param name="sum"></param> | |
/// <param name="dictionary"></param> | |
private static void addRootToLeafPathSum(Tree root, int sum, Dictionary<int, int> dictionary) | |
{ | |
if (root == null) | |
{ | |
return; | |
} | |
// base case - leaf node - no child | |
bool noChild = root.Left == null && root.Right == null; | |
var nextSum = sum + root.Value; | |
if (noChild) | |
{ | |
if (dictionary.ContainsKey(nextSum)) | |
{ | |
dictionary[nextSum] += 1; | |
} | |
else | |
{ | |
dictionary.Add(nextSum, 1); | |
} | |
return; | |
} | |
if (root.Left != null) | |
{ | |
addRootToLeafPathSum(root.Left, nextSum, dictionary); // check the count | |
} | |
if (root.Right != null) | |
{ | |
addRootToLeafPathSum(root.Right, nextSum, dictionary); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment