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 :



title-img






                        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

Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →

Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →

Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →

Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →

Reverse a linked list

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

View Solution →