Search code examples
pathtreered-black-tree

How to know if a tree is colorable (RB Tree)


is there an algorithm to know if a tree is colorable or not? Because I found this phrase on Wikipedia:

The path from the root to the farthest leaf is no more than twice as long as the path from the root to the nearest leaf.

So, for example, taking in exam these two trees: enter image description here

In the first one the shortest path from the root is the one on the left and it has a distance of 1 from the root. The longest one is on the right and has a distance of 3. So 3 is not <= 2*1, so the tree on the left is not colorable, right? In the second tree the shortest path takes 2 nodes and the fastest takes 2 nodes too. 2 <= 2*2 so it is colorable I guess.


Solution

  • Exactly, to know if a tree is colorable or not you must count from the root (excluding it) and then go through the longest path. Then, you will have the answer