Skip to content

Commit e9b49b4

Browse files
committed
upload
1 parent 5a2b842 commit e9b49b4

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
## Rotate Linked List
2+
Given a binary tree, find its height. [🔗Goto](https://practice.geeksforgeeks.org/problems/height-of-binary-tree/1/?page=2&difficulty[]=1&status[]=unsolved&company[]=Amazon&company[]=Infosys&sortBy=submissions#)
3+
4+
<details>
5+
<summary>Full Code</summary>
6+
7+
```
8+
import java.util.LinkedList;
9+
import java.util.Queue;
10+
import java.io.*;
11+
import java.util.*;
12+
import java.lang.*;
13+
14+
class Node{
15+
int data;
16+
Node left;
17+
Node right;
18+
Node(int data){
19+
this.data = data;
20+
left=null;
21+
right=null;
22+
}
23+
}
24+
25+
class GFG {
26+
27+
static Node buildTree(String str){
28+
29+
if(str.length()==0 || str.charAt(0)=='N'){
30+
return null;
31+
}
32+
33+
String ip[] = str.split(" ");
34+
// Create the root of the tree
35+
Node root = new Node(Integer.parseInt(ip[0]));
36+
// Push the root to the queue
37+
38+
Queue<Node> queue = new LinkedList<>();
39+
40+
queue.add(root);
41+
// Starting from the second element
42+
43+
int i = 1;
44+
while(queue.size()>0 && i < ip.length) {
45+
46+
// Get and remove the front of the queue
47+
Node currNode = queue.peek();
48+
queue.remove();
49+
50+
// Get the current node's value from the string
51+
String currVal = ip[i];
52+
53+
// If the left child is not null
54+
if(!currVal.equals("N")) {
55+
56+
// Create the left child for the current node
57+
currNode.left = new Node(Integer.parseInt(currVal));
58+
// Push it to the queue
59+
queue.add(currNode.left);
60+
}
61+
62+
// For the right child
63+
i++;
64+
if(i >= ip.length)
65+
break;
66+
67+
currVal = ip[i];
68+
69+
// If the right child is not null
70+
if(!currVal.equals("N")) {
71+
72+
// Create the right child for the current node
73+
currNode.right = new Node(Integer.parseInt(currVal));
74+
75+
// Push it to the queue
76+
queue.add(currNode.right);
77+
}
78+
i++;
79+
}
80+
81+
return root;
82+
}
83+
static void printInorder(Node root){
84+
if(root == null)
85+
return;
86+
87+
printInorder(root.left);
88+
System.out.print(root.data+" ");
89+
90+
printInorder(root.right);
91+
}
92+
93+
public static void main (String[] args) throws IOException{
94+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
95+
int t=Integer.parseInt(br.readLine());
96+
97+
while(t > 0){
98+
String s = br.readLine();
99+
Node root = buildTree(s);
100+
101+
Solution ob = new Solution();
102+
System.out.println(ob.height(root));
103+
t--;
104+
}
105+
}
106+
}
107+
```
108+
</details>
109+
110+
```
111+
class Solution {
112+
//Function to find the height of a binary tree.
113+
int height(Node node)
114+
{
115+
// code here
116+
if(node == null){
117+
return 0;
118+
}
119+
int l = height(node.left);
120+
int r = height(node.right);
121+
if (l > r)
122+
return (l + 1);
123+
else
124+
return (r + 1);
125+
}
126+
}
127+
```

0 commit comments

Comments
 (0)