Created
November 20, 2016 23:08
-
-
Save jianminchen/c910f5d1f37309c70b489e6c75b0678c to your computer and use it in GitHub Desktop.
Minimum Loss - binary tree - HackerRank - woman codesprint #2 study code - binary tree implementation -
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.IO; | |
class Solution { | |
public class Node { | |
public Node L {get; set;} | |
public Node R {get; set;} | |
public long Value {get; set;} | |
} | |
public static long Insert (Node root, long value, long prev) { | |
if (root.Value <= value ) { | |
if (root.R == null) { | |
root.R = new Node {Value = value}; | |
return prev; | |
} else { | |
return Math.Min(Insert(root.R, value, prev), prev); | |
} | |
} else { | |
var temp = root.Value - value; | |
if (root.L == null) { | |
root.L = new Node {Value = value}; | |
return Math.Min(temp, prev); | |
} else { | |
return Insert(root.L, value, Math.Min(temp, prev)); | |
} | |
} | |
} | |
static void Main(String[] args) { | |
string[] tokens_s = Console.ReadLine().Split(' '); | |
string[] price_temp = Console.ReadLine().Split(' '); | |
long[] prices = Array.ConvertAll(price_temp,Int64.Parse); | |
var result = long.MaxValue; | |
var root = new Node {Value = prices[0]}; | |
for (var i = 1; i < prices.Length; i++) { | |
result = Insert(root, prices[i], result); | |
} | |
Console.WriteLine(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment