Height of Spiral Tree | Practice | GeeksforGeeks

Given a special Binary Tree whose leaf nodes are connected to form a circular doubly linked list. Find the height of this special Binary Tree.
Example 1:
Input:
        1
      /   \
     2     3
    /
   4
Output: 3
â€‹Explanation: There are 3 edges and 4
nodes in spiral tree where leaf nodes
4 and 3 are connected in circular
doubly linked list form. Thus the
height of spiral tree is 3.

Example 2:
Input:
        1
      /   \
     2     3
    / \
   4   5
  /
 6
Output: 4
Your Task:
You don't need to read input or print anything. Your task is to complete the function findTreeHeight() which takes the root of the special Binary Tree as its input and returns the Height of this special Binary Tree.
In a special Binary Tree, the leaf nodes form a circular doubly linked list.
For Example:
      1
     /   \ 
    2    3
   /  \
  4  5
 /  
6 
In the above binary tree, 6, 5 and 3 are leaf nodes and they form a circular doubly linked list. Here, the left pointer of leaf node will act as a previous pointer of circular doubly linked list and its right pointer will act as next pointer of circular doubly linked list.
Expected Time Complexity: O(Height of the Tree).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1 <= Number of nodes <= 104
0
danish_nitdgp2 weeks ago
Simple Solutionint findTreeHeight(Node* root){  if(root == NULL) return 0;  if(root->left && root->right){   if(root->left->right == root && root->right->left == root) return 1;  }  return 1+ max(findTreeHeight(root->left), findTreeHeight(root->right));}
0
bishtmegha982 months ago
class Pair {
    Node node;
    int dist;
    public Pair(Node n, int d) {
        node = n;
        dist = d;
    }
}
class Tree
{
    // Return the height of the given special binary tree
    public static int findTreeHeight(Node root) {
        // code here.
        Queue<Pair> aux = new LinkedList<>();
        
        aux.add(new Pair(root,0));
        int height = 0;
        
        while(!aux.isEmpty()) {
            Pair p = aux.poll();
            Node curr = p.node;
            int d = p.dist;
            
            height = Math.max(height, d);
            
            if (curr.left != null) {
                    aux.add(new Pair(curr.left, d+1));
                    curr.left = null;
            }
                
            if (curr.right != null) {
                    aux.add(new Pair(curr.right, d+1));
                    curr.right = null;
            }
        }
        return height;
    }
}
-2
bunny1236942 months ago
BFS with visited 
 
   public static int findTreeHeight(Node root)
        {
            HashSet<Node> vis = new HashSet<>();  
            Queue<Node>q = new ArrayDeque<>();
            
            q.add(root); 
            vis.add(root); 
             int level = 0;
            while(q.size()!=0){
                int size = q.size();
                while(size-- > 0){
                    Node rem = q.remove(); 
                    if(rem.left != null && !vis.contains(rem.left)){
                        q.add(rem.left); 
                        vis.add(rem.left); 
                    }
                    if(rem.right!=null && !vis.contains(rem.right)){
                        q.add(rem.right); 
                        vis.add(rem.right); 
                    }
                }
                level++; 
            }
            
            return level; 
        }
0
rohithsowmithra3 months ago
def findTreeHeight(root):   #code here   q = deque()   q.append(root)   height = 0      while q:       cur_size = len(q)       while cur_size > 0:           node = q.popleft()           cur_size -= 1                      if node.left:               if node.left.right != node:                   q.append(node.left)           if node.right:               if node.right.left != node:                   q.append(node.right)                             height += 1   return height
+1
aayushiiiisingh4 months ago
int findTreeHeight(Node* root){   if(root == NULL)       return 0;Node* temp = root->left;Node* temp1 = root->right;if(root->left && root->right){    if(temp->right == root && temp1->left == root)        return 1;}return 1+ max(findTreeHeight(root->left), findTreeHeight(root->right));}
0
amitzala544
This comment was deleted.
0
peeyush_thakur9 months ago
peeyush_thakur
Execution time:0.2sint findTreeHeight(Node* root){    if(root==NULL)    return 0;    if(root->left && root->right){if(root->left->right==root && root->right->left==root)return 1;}int lh=findTreeHeight(root->left);int rh=findTreeHeight(root->right);return 1+max(lh,rh);}
+4
Debojyoti Sinha1 year ago
Debojyoti Sinha
Correct Answer.Correct AnswerExecution Time:0.20
int findTreeHeight(Node* root){    if(root)    {        if(root->left and root->left->right == root)        {            return 1;        }                int lHeight = findTreeHeight(root->left);        int rHeight = findTreeHeight(root->right);                return 1 + max(lHeight, rHeight);    }    else    {        return 0;    }}
0
G Vishal1 year ago
G Vishal
a different approachhttps://ide.geeksforgeeks.o...https://uploads.disquscdn.c...
0
Yash Patil1 year ago
Yash Patil
public static int findTreeHeight(Node root)    {            if(root==null)            {                return 0;            }        return height(root);    }    public static boolean isLeaf(Node root)    {        return (root.left!=null&&root.left.right==root&&root.right!=null&&root.right.left==root);    }    public static int height(Node root)    {        if(root==null)        {            return 0;        }        if(isLeaf(root)==true)        {            return 1;        }
        int left=height(root.left);        int right=height(root.right);        return 1+Math.max(left,right);    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.