Ways to Remove Leaves of a Tree - Google Top Interview Questions
Problem Statement :
You are given a binary tree root. In one operation you can delete one leaf node in the tree. Return the number of different ways there are to delete the whole tree. Constraints 0 ≤ n ≤ 100,000 Example 1 Input Visualize root = [1, [2, null, null], [3, [4, null, null], null]] Output 3 Explanation The three ways to delete are [2, 4, 3, 1] [4, 2, 3, 1] [4, 3, 2, 1] Solved 92 Attempted 127
Solution :
Solution in Java :
import java.util.*;
/**
* public class Tree {
* int val;
* Tree left;
* Tree right;
* }
*/
class Solution {
static long[][] rd = new long[100001][101];
static final int MODE = 1000000007;
static {
for (int i = 0; i < rd.length; ++i) {
rd[i][0] = 1;
for (int j = 1; j < i && j < rd[0].length; ++j) {
rd[i][j] = (rd[i - 1][j - 1] + rd[i - 1][j]) % MODE;
}
if (i < rd[0].length)
rd[i][i] = 1;
}
}
public int solve(Tree root) {
int[] res = todo(root);
return res[0];
}
private int[] todo(Tree root) {
if (root == null)
return new int[] {1, 0};
int[] l = todo(root.left), r = todo(root.right);
long t;
if (l[1] <= r[1]) {
t = rd[l[1] + r[1]][l[1]];
} else {
t = rd[l[1] + r[1]][r[1]];
}
t *= l[0] * r[0];
l[0] = (int) (t % MODE);
l[1] += r[1] + 1;
return l;
}
}
Solution in Python :
class Solution:
def solve(self, root):
def rec(root): # return (size, number of ways) for current subtree
if root is None: # Root is null
return (0, 1)
leftsize, leftdp = rec(root.left)
rightsize, rightdp = rec(root.right)
return (
leftsize + rightsize + 1,
comb(leftsize + rightsize, leftsize) * leftdp * rightdp,
)
return rec(root)[1]
themast3r
95
Big N
6 months ago
Came up with an exact solution lol. Was thinking it's a elegant solution, so, I should post XD
class Solution:
def solve(self, root):
def helper(root):
if not root:
return [0, 1]
leftSize, leftWays, rightSize, rightWays = helper(root.left) + helper(root.right)
return [ leftSize + rightSize + 1, leftWays * rightWays * math.comb(leftSize + rightSize, leftSize) ]
return helper(root).pop()
```
View More Similar Problems
Down to Zero II
You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.
View Solution →Truck Tour
Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr
View Solution →Queries with Fixed Length
Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon
View Solution →QHEAP1
This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element
View Solution →Jesse and Cookies
Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t
View Solution →Find the Running Median
The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.
View Solution →