Skip to content

Commit

Permalink
check in the code
Browse files Browse the repository at this point in the history
  • Loading branch information
jianminchen committed Aug 9, 2018
1 parent b10f891 commit 2fb1b0f
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Leetcode501_FindModeInBST
{
/// <summary>
/// Leetcode 501 - Find mode in BST
/// Study discussion panel:
/// https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/98101/Proper-O(1)-space
/// Simplify the logic, every visit of node - update the mode
/// </summary>
class Program
{
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}

static void Main(string[] args)
{
//RunSampleTestcase();
RunTestCase();
}

public static void RunSampleTestcase()
{
var root1 = new TreeNode(1);
var root2 = new TreeNode(2);
var root2B = new TreeNode(2);
root1.right = root2;
root2.left = root2B;

var result = FindMode(root1);
}

public static void RunTestCase()
{
var root1 = new TreeNode(1);
var root2 = new TreeNode(2);

root1.right = root2;

var result = FindMode(root1);
}

public static TreeNode previous;
public static TreeNode current;
public static int max = 0;

public static int currentCount = 0;
public static HashSet<int> numbers;

public static int[] FindMode(TreeNode root)
{
previous = null;
current = null;
max = 0;

currentCount = 0;
numbers = new HashSet<int>();

inorderTraversal(root);

return numbers.ToArray();
}

/// <summary>
/// inorder traversal - left, root, right
/// </summary>
/// <param name="root"></param>
private static void inorderTraversal(TreeNode root)
{
if (root == null)
{
return;
}

inorderTraversal(root.left);

// handling mode rules
current = root;
var sameAsPrevious = previous != null && previous.val == current.val;
var startNew = previous == null || previous.val != current.val;

if (sameAsPrevious)
{
currentCount++;
}

if (startNew)
{
currentCount = 1;
}

// every node the mode is checked to simplify the code logic
updateMode(current.val);

// set current to previous
previous = current;

inorderTraversal(root.right);
}

/// <summary>
///
/// </summary>
private static void updateMode(int candidate)
{
// summarize the last value
if (currentCount > max)
{
max = currentCount;

numbers.Clear();
numbers.Add(candidate);
}
else if (currentCount > 0 && currentCount == max)
{
numbers.Add(candidate);
}
}
}
}

0 comments on commit 2fb1b0f

Please sign in to comment.