forked from jianminchen/Leetcode_Julia
-
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
1 parent
b10f891
commit 2fb1b0f
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
... tree/Leetcode 501 Find mode in binary search tree - in order traversal - ShortVersion.cs
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,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); | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.