Can anyone help to to create a binary tree and do a non recursive preorder traversal of the binary tree in c?


Question: check if a string has four consecutive letters in ascending or descending order
Answer: This is my solution. It uses only a single loop.

Keep in mind that you'll need more logic if you want to constrain it to pure ASCII.

static boolean isWeak(String pass) {
  Character prev = null;
  Boolean asc = null;
  int streak = 0;
  for (char c : pass.toCharArray()) {
    if (prev != null) {
      switch (c - prev) {
      case -1:
        if (Boolean.FALSE.equals(asc)) streak++;
        else { asc = false; streak = 2; }
        break;
      case 1:
        if (Boolean.TRUE.equals(asc)) streak++;
        else { asc = true; streak = 2; }
        break;
      default: asc = null; streak = 0;
      }
      if (streak == 4) return true;
    }
    prev = c;
  }
  return false;
}