This is an invalid binary tree as input. You can't have the 5 on the end. You need to have null, 5 or 5, null. Your input is wrong.