How to Find Binary Tree Violations and Longest Slide?

Modified on Tue, 12 Sep, 2023 at 10:56 AM

How do I find violations in a binary tree?

Do you understand the problem?

The problem statement asks us to find the violations in a binary tree.

What are the things you should already know?

Do you know what a balanced binary tree is? And how to traverse a binary tree?

What do you think the problem statement is talking about?

A binary tree violation occurs when a binary tree does not satisfy the rules of a binary tree. 

How to approach this? To solve this, I would recommend the following steps:

To find the number of violations in a tree you need to check with the condition of a balanced binary tree.


You have to check the left height and right height at each node and check if there is a height violation there, and increment the count. At every node, you have to check abs(left height - right height) > 1 and increment the count as if it is greater than 1 it is considered as a violation of the rule.



How do I find the longest slide?

Do you understand the problem?

The problem statement asks us to find the longest slide in a tree.

What are the things you should already know?

Do you know what a slide is and how to traverse to find this?

What do you think the problem statement is talking about?

To find the slide we would have to traverse each direction and find the current longest slide as we traverse.

How to approach this? To solve this, I would recommend the following steps:

The longest slide is the longest distance you can travel on a tree while only going one direction, so we need to pick a variable for checking which direction we are going and call it recursively to travel both left and right directions. update each time with max of length we have traveled in the left and right directions. 

For example we can use 1 to traverse in the left direction and 2 to traverse the right direction, and each place we calculate the height and store the max values.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article