logo CodeStepByStep logo

isConsecutive

Language/Type: C++ binary trees pointers recursion

Write a function named isConsecutive that accepts a pointer to a BinaryTreeNode representing the root of a binary tree. Your function should examine the tree and return true if there is some integer value k such that an in-order traversal of the tree's elements would yield a sequence of consecutive integers k, k+1, k+2, ... where the integers differ by exactly 1 from their neighbors in the sequence; otherwise the function should return false. Recall that an in-order traversal visits nodes in left-center-right order. For example, suppose variables named tree1, tree2, ..., tree6 point to the roots of trees that store the trees of elements below.

The call of isConsecutive(tree1) would return true because an in-order traversal of tree1 produces the sequence of integers 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, which are consecutive integers from 4 through 14. The call of isConsecutive(tree2) would return true because an in-order traversal of tree2 produces the sequence of integers 41, 42, 43, 44, 45, which are consecutive integers from 41 through 45. The call of isConsecutive(tree3) would return false because an in-order traversal of tree3 produces the sequence of integers 9, 7, 38, 20, 15, which is not a sequence of consecutive integers. The call of isConsecutive(tree4) would return false because an in-order traversal of tree4 produces the sequence of integers 2, 3, 5, 6, 7, 9, 12, 15, which is not a sequence of consecutive integers. (It is ascending, but not consecutive.) The call of isConsecutive(tree5) would return false because an in-order traversal of tree5 produces the sequence of integers 2, 1, 3, 4, which is not a sequence of consecutive integers. (The integers could be rearranged into a consecutive sequence, but an in-order traversal does not produce a consecutive sequence.) The call of isConsecutive(tree6) would return false because an in-order traversal of tree6 produces the sequence of integers 1, 2, 3, 4, 5, 6, 8, 9, which is not a sequence of consecutive integers (missing a 7).

tree1
         8
      /     \
    7         11
   /        /    \
  5        9      13
 / \        \    /  \
4   6       10  12  14
tree2
   42
  /  \
41    45
     /
   43
     \
      44
tree3
   7
 /   \
9     15
     /
   20
  /
38
tree4
         6
      /     \
    5         15
   /        /
  3        9
 /        / \
2        7   12
tree5
  1
 / \
2   4
   /
  3
tree6
        4
      /   \
    3       6
   /       / \
  1       5   9
   \         /
    2       8

If the tree is empty or contains only a single element, your function should return true.

Your solution should be efficient. Specifically, you should not traverse over the same nodes or subtrees multiple times. You also should not explore regions of the tree if you do not need to. Once your code knows for sure whether the tree could or could not be consecutive, your algorithm should stop without exploring further.

Constraints: You must implement your function recursively and without using loops. Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). Your function should not modify the tree's state; the state of the tree should remain constant with respect to your function. Don't modify root, or a node's data, left, or right pointers. Do not construct any new BinaryTreeNode objects in solving this problem (though you may create as many BinaryTreeNode* pointer variables as you like). Do not leak memory. You should not be allocating dynamic memory or creating new node objects anyway. Your solution should be at worst O(N) time, where N is the number of elements in the tree. You must also solve the problem using a single pass over the tree, not multiple passes.

Assume that you are using the BinaryTreeNode structure as defined below:

struct BinaryTreeNode {
    int data;
    BinaryTreeNode* left;
    BinaryTreeNode* right;
};
Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.