Created
January 19, 2019 17:51
-
-
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
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.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