Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 19, 2019 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/fea7cfa47ea87df99b7840c5c14f301a to your computer and use it in GitHub Desktop.
Save jianminchen/fea7cfa47ea87df99b7840c5c14f301a to your computer and use it in GitHub Desktop.
C# - Jan. 18, 2019 - interviewing.io - mock interview solution written by the interviewee - a bug uncovered
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _687_longest_univalue_path_II
{
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
class Program
{
static void Main(string[] args)
{
}
/// <summary>
/// Jan. 19, 2019
/// The design has a problem
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
public static int LongestUnivaluePath(TreeNode root) {
if (root == null || (root.left == null && root.right == null))
return 0;
if (root.left != null && root.val == root.left.val && root.right != null && root.val == root.right.val)
{
return 2 + LongestUnivaluePath(root.left) + LongestUnivaluePath(root.right);
}
var left = LongestUnivaluePath(root.left);
var right = LongestUnivaluePath(root.right);
if(root.left != null && root.val == root.left.val)
{
left++;
}
if(root.right != null && root.val == root.right.val)
{
right++;
}
return Math.Max(left, right);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment