3 Length Paths

Given a tree A having N nodes, find and return the minimum number of colors needed to color all the nodes of the tree such that all paths of length <= 3 in the tree have distinct colors.

Input Format:

The first and the only argument of input contains a 2-d matrix A, of size (N - 1) x 2, where node A[i][0] and node A[i][1] are connected.

Output Format:

Return an integer representing the answer.

Constraints:

2 <= N <= 1e5
1 <= A[i] <= N
Nodes are numbered 1 to N.

Examples:

Input 1:
    A = [   [1, 2],
            [2, 3]  ]
    
Output 1:
    3

Explanation 1:
    Node | Color
     1   |   1
     2   |   2
     3   |   3
    
    You can check that 3 is the least possible number of colors that can be used.

Input 2:
    A = [   [1, 2],
            [2, 3],
            [2, 4],
            [4, 5]  ]

Output 2:
    4
    
Explanation 2:
    
    Node | Color
     1   |   1
     2   |   2
     3   |   3
     4   |   4
     5   |   1
    
    You can check that 4 is the least possible number of colors that can be used.
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving 3 Length Paths on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions