Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6376 |

@mb1973 | 5594 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3412 |

Complete Leaderboard | |

Handle | Score |

@codersgroup18 | 783 |

@Manikanta punnam | 605 |

@sriramgoparaju99 | 566 |

@praveenbgp6 | 529 |

@rdakka | 526 |

@prankursharma31 | 518 |

@yashkaril4 | 517 |

@purohitmn02 | 467 |

@sonamnigam1999 | 443 |

@sonamkumari63928 | 441 |

@shubhamstudent5 | 433 |

Complete Leaderboard |

Given a binary tree and a integer value **K**, the task is to **find all nodes** data in given binary tree** having exactly K leaves **in **sub-tree rooted with them**.

**NOTE: **Nodes should be printed in the order in which they appear in postorder traversal.

**Example 1:**

**Input:
**K = 1
0
/ \
1 2**
Output: **-1**
Explanation: **There is no node in this
tree which has one leaf in the sub tree
below it.

**Example 2:**

**Input:
**K = 2
0
/ \
1 2
/
4
/ \
5 9**
Output: **4 2**
Explanation: **Nodes with data 2 and 4, have
2 leaves in the subtree below it.

**Your Task:**

Just complete the function **btWithKleaves() **and returns the answer (as a vector in **cpp, **as a ArrayList in **java **and as list in **python)**

**Note: **If no node is found the list returned should contain only one value -1.

**Expected Time Complexity: **O(N).

**Expected Auxiliary Space: **O(Height of the Tree).

**Constraints:**

1 <= N <= 1000

1 <= K <= 1000

1 <= value of nodes <= 10000

**Note:**The **Input/Ouput** format and **Example** given are used for system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Print Nodes having K leaves

...