# Check if level-wise Decimal equivalent of Binary Tree forms a Monotonic sequence or not

• Last Updated : 09 Sep, 2022

Given the root of a binary tree in which all nodes has values either 0 or 1, the task is to check if the level-wise decimal equivalent of given Tree forms a monotonic sequence.or not.

A sequence is monotonic if it is either monotone increasing or monotone decreasing.
A sequence nums is monotone increasing if for all i <= j, nums[i] <= nums[j].
A sequence nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].

Examples:

Input:
0
/   \
0     1
/      /
1     0
/   \
1     1

Output:
Yes
Explanation
Level 0: 0  -> 0
Level 1: 01 -> 1
Level 2: 10 -> 2
Level 3: 11 -> 3
The sequence formed from the decimal equivalent of each level from root to leaf is {0, 1, 2, 3} which is a monotone increasing sequence.
Input:
1
/   \
1     1
/      /
1     0
/   \
0     1

Output:
No

Approach:

The idea is to perform levelorder traversal of given Tree and for each level, convert the binary representation to decimal and store in an int variable. Then the problem will be converted to simply check if given array is a monotonic sequence or not.

Follow the below steps to solve this problem:

• Do level order traversal from root to leaf
• For each level, convert it to decimal equivalent and store the value in an integer array
• Then check whether the given array is monotonic increasing or decreasing

Below is the implementation of the above approach:

## C#

 `// C# code to check if the tree is monotonic` `using` `System;` `using` `System.Collections.Generic;` `using` `System.Linq;`   `// Class containing left and right` `// child of current node and key value` `public` `class` `Node {` `    ``public` `int` `data;` `    ``public` `Node left, right;` `    ``public` `Node(``int` `item)` `    ``{` `        ``data = item;` `        ``left = right = ``null``;` `    ``}` `}`   `public` `class` `GFG {` `    ``// Root of the Binary Tree` `    ``public` `Node root;`   `    ``public` `virtual` `bool` `checkMonotonic(Node root)` `    ``{` `        ``int``[] sequenceArray = getLevelOrderArray(root);` `        ``bool` `increasing = ``true``;` `        ``bool` `decreasing = ``true``;` `        ``for` `(``int` `i = 0; i < sequenceArray.Length - 1; ++i) {` `            ``if` `(sequenceArray[i] > sequenceArray[i + 1])` `                ``increasing = ``false``;` `            ``if` `(sequenceArray[i] < sequenceArray[i + 1])` `                ``decreasing = ``false``;` `        ``}` `        ``return` `increasing || decreasing;` `    ``}`   `    ``public` `virtual` `int``[] getLevelOrderArray(Node root)` `    ``{` `        ``int` `h = height(root);` `        ``int` `i;` `        ``List<``int``> retVal = ``new` `List<``int``>();`   `        ``for` `(i = 1; i <= h; i++) {` `            ``List<``int``> currentLevel = ``new` `List<``int``>();` `            ``var` `currentLevelOrder` `                ``= getCurrentLevel(root, i, currentLevel)` `                      ``.ToList();` `            ``retVal.Add(Convert.ToInt32(` `                ``string``.Join(``""``, currentLevelOrder), 2));` `        ``}` `        ``return` `retVal.ToArray();` `    ``}`   `    ``// Compute the "height" of a tree --` `    ``// the number of nodes along the longest` `    ``// path from the root node down to the` `    ``// farthest leaf node.` `    ``public` `virtual` `int` `height(Node root)` `    ``{` `        ``if` `(root == ``null``) {` `            ``return` `0;` `        ``}` `        ``else` `{` `            ``// compute height of each subtree` `            ``int` `lheight = height(root.left);` `            ``int` `rheight = height(root.right);`   `            ``// use the larger one` `            ``if` `(lheight > rheight) {` `                ``return` `(lheight + 1);` `            ``}` `            ``else` `{` `                ``return` `(rheight + 1);` `            ``}` `        ``}` `    ``}`   `    ``// Get nodes at the current level` `    ``public` `virtual` `IList<``int``>` `    ``getCurrentLevel(Node root, ``int` `level,` `                    ``List<``int``> currentLevelOrder)` `    ``{` `        ``if` `(root == ``null``) {` `            ``return` `currentLevelOrder;` `        ``}` `        ``if` `(level == 1) {` `            ``currentLevelOrder.Add(root.data);` `        ``}` `        ``else` `if` `(level > 1) {` `            ``getCurrentLevel(root.left, level - 1,` `                            ``currentLevelOrder);` `            ``getCurrentLevel(root.right, level - 1,` `                            ``currentLevelOrder);` `        ``}` `        ``return` `currentLevelOrder;` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main(``string``[] args)` `    ``{` `        ``GFG tree = ``new` `GFG();` `        ``tree.root = ``new` `Node(0);` `        ``tree.root.left = ``new` `Node(0);` `        ``tree.root.right = ``new` `Node(1);` `        ``tree.root.left.left = ``new` `Node(1);` `        ``tree.root.right.left = ``new` `Node(0);` `        ``tree.root.right.left.left = ``new` `Node(1);` `        ``tree.root.right.left.right = ``new` `Node(1);`   `        ``bool` `ans = tree.checkMonotonic(tree.root);` `        ``if` `(ans)` `            ``Console.Write(``"Yes"``);` `        ``else` `            ``Console.Write(``"No"``);` `    ``}` `}`

## Javascript

 ``

Output

`Yes`

Time Complexity: O(N^2)
Auxiliary Space: O(N+L), where L is the count of levels in Tree, which will the size of Array to store decimal value of each level

Efficient Approach: The auxiliary space in the above approach can be optimised by just looking at adjacent levels and checking they follow monotonic sequence or not.

Instead of storing the decimal equivalent of each level in a array and then check the array for monotonic, we can check the increasing/decreasing nature of each level as we traverse it. This way, we can terminate as soon as we get a value that breaks the monotonicity. For this to work, we will need to store the value of previous level to compare with the next level.

Below is the implementation of the above approach:

## C#

 `// C# code to implement the above approach` `using` `System;` `using` `System.Collections.Generic;` `using` `System.Linq;`   `// Class containing left and right` `// child of current node and key value` `public` `class` `Node {` `    ``public` `int` `data;` `    ``public` `Node left, right;` `    ``public` `Node(``int` `item)` `    ``{` `        ``data = item;` `        ``left = right = ``null``;` `    ``}` `}`   `public` `class` `GFG {` `    ``// Root of the Binary Tree` `    ``public` `Node root;`   `    ``public` `virtual` `bool` `checkMonotonic(Node root)` `    ``{` `        ``int` `h = height(root);` `        ``int` `i;` `        ``int` `prevLevelValue` `            ``= Convert.ToInt32(root.data.ToString(), 2);`   `        ``bool` `increasing = ``true``;` `        ``bool` `decreasing = ``true``;`   `        ``for` `(i = 1; i <= h; i++) {` `            ``List<``int``> currentLevel = ``new` `List<``int``>();` `            ``var` `currentLevelOrder` `                ``= getCurrentLevel(root, i, currentLevel)` `                      ``.ToList();` `            ``int` `currentLevelValue = Convert.ToInt32(` `                ``string``.Join(``""``, currentLevelOrder), 2);`   `            ``if` `(prevLevelValue > currentLevelValue)` `                ``increasing = ``false``;` `            ``if` `(prevLevelValue < currentLevelValue)` `                ``decreasing = ``false``;`   `            ``prevLevelValue = currentLevelValue;` `        ``}` `        ``return` `increasing || decreasing;` `    ``}`   `    ``// Compute the "height" of a tree --` `    ``// the number of nodes along the longest` `    ``// path from the root node down to the` `    ``// farthest leaf node.` `    ``public` `virtual` `int` `height(Node root)` `    ``{` `        ``if` `(root == ``null``) {` `            ``return` `0;` `        ``}` `        ``else` `{` `            ``// compute height of each subtree` `            ``int` `lheight = height(root.left);` `            ``int` `rheight = height(root.right);`   `            ``/* use the larger one */` `            ``if` `(lheight > rheight) {` `                ``return` `(lheight + 1);` `            ``}` `            ``else` `{` `                ``return` `(rheight + 1);` `            ``}` `        ``}` `    ``}`   `    ``// Get nodes at the current level` `    ``public` `virtual` `IList<``int``>` `    ``getCurrentLevel(Node root, ``int` `level,` `                    ``List<``int``> currentLevelOrder)` `    ``{` `        ``if` `(root == ``null``) {` `            ``return` `currentLevelOrder;` `        ``}` `        ``if` `(level == 1) {` `            ``currentLevelOrder.Add(root.data);` `        ``}` `        ``else` `if` `(level > 1) {` `            ``getCurrentLevel(root.left, level - 1,` `                            ``currentLevelOrder);` `            ``getCurrentLevel(root.right, level - 1,` `                            ``currentLevelOrder);` `        ``}` `        ``return` `currentLevelOrder;` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main(``string``[] args)` `    ``{` `        ``GFG tree = ``new` `GFG();` `        ``tree.root = ``new` `Node(0);` `        ``tree.root.left = ``new` `Node(0);` `        ``tree.root.right = ``new` `Node(1);` `        ``tree.root.left.left = ``new` `Node(1);` `        ``tree.root.right.left = ``new` `Node(0);` `        ``tree.root.right.left.left = ``new` `Node(1);` `        ``tree.root.right.left.right = ``new` `Node(1);`   `        ``bool` `ans = tree.checkMonotonic(tree.root);` `        ``if` `(ans)` `            ``Console.Write(``"Yes"``);` `        ``else` `            ``Console.Write(``"No"``);` `    ``}` `}`

Output

`Yes`

Time Complexity: O(N^2)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles