A tree is Continuous tree if in each root to leaf path, absolute difference between keys of two adjacent is 1. We are given a binary tree, we need to check if tree is continuous or not.
Input : 3 / \ 2 4 / \ \ 1 3 5 Output: "Yes" // 3->2->1 every two adjacent node's absolute difference is 1 // 3->2->3 every two adjacent node's absolute difference is 1 // 3->4->5 every two adjacent node's absolute difference is 1 Input : 7 / \ 5 8 / \ \ 6 4 10 Output: "No" // 7->5->6 here absolute difference of 7 and 5 is not 1. // 7->5->4 here absolute difference of 7 and 5 is not 1. // 7->8->10 here absolute difference of 8 and 10 is not 1.
The solution requires a traversal of tree. The important things to check are to make sure that all corner cases are handled. The corner cases include, empty tree, single node tree, a node with only left child and a node with only right child.
In tree traversal, we recursively check if left and right subtree are continuous. We also check if difference between keys of current node’s key and its children keys is 1. Below is the implementation of the idea.
This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Convert a given Binary tree to a tree that holds Logical AND property
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Convert a given Binary tree to a tree that holds Logical OR property
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Remove all leaf nodes from a Generic Tree or N-ary Tree
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Sub-tree with minimum color difference in a 2-coloured tree
- Cartesian tree from inorder traversal | Segment Tree
- Create a mirror tree from the given binary tree
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Difference between General tree and Binary tree
- Interval Tree using GNU Tree-based container
Improved By : andrew1234