Skip to content

Commit 1880c73

Browse files
Add files via upload
add
1 parent 3bc9047 commit 1880c73

8 files changed

+294
-0
lines changed

F-Tree/Daimeter.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
// Function to return the diameter of a Binary Tree.
3+
int diameter(Node root) {
4+
// Your code here
5+
int max[] = new int[1];
6+
height(root, max);
7+
return (max[0]);
8+
}
9+
private int height(Node node, int max[]){
10+
if(node == null) {
11+
return 0;
12+
}
13+
int l = height(node.left, max);
14+
15+
int r = height(node.right, max);
16+
max[0] = Math.max(max[0], l+r);
17+
return 1+Math.max(l,r);
18+
}
19+
}

F-Tree/Height_Of_Tree.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Height of Binary Tree
3+
Difficulty: EasyAccuracy: 78.58%Submissions: 293K+Points: 2
4+
Given a binary tree, find its height.
5+
6+
The height of a tree is defined as the number of edges on the longest path from the root to a leaf node. A leaf node is a node that does not have any children.
7+
8+
Examples:
9+
10+
Input: root[] = [12, 8, 18, 5, 11]
11+
12+
Output: 2
13+
Explanation: The longest path from the root (node 12) goes through node 8 to node 5, which has 2 edges.
14+
*/
15+
16+
import org.w3c.dom.Node;
17+
18+
class Solution {
19+
// Function to find the height of a binary tree.
20+
int height(Node node) {
21+
// code here
22+
if (node == null) {
23+
return 0;
24+
}
25+
if (node != null && node.left == null && node.right==null) {
26+
return 0;
27+
}
28+
29+
int leftH = height(node.left);
30+
int rightH = height(node.right);
31+
32+
return Math.max(leftH, rightH) + 1;
33+
}
34+
}

F-Tree/InOrder.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
/*
3+
1. Inorder Traversal
4+
Difficulty: BasicAccuracy: 67.15%Submissions: 162K+Points: 1
5+
Given a Binary Tree, find the In-Order Traversal of it.
6+
7+
Examples:
8+
9+
Input:
10+
1
11+
/ \
12+
3 2
13+
Output: [3, 1, 2]
14+
Explanation: The in-order traversal of the given binary tree is [3, 1, 2].
15+
Input:
16+
10
17+
/ \
18+
20 30
19+
/ \ /
20+
40 60 50
21+
Output: [40, 20, 60, 10, 50, 30]
22+
Explanation: The in-order traversal of the given binary tree is [40, 20, 60, 10, 50, 30].
23+
*/
24+
import java.util.*;
25+
26+
import org.w3c.dom.Node;
27+
28+
class Solution {
29+
30+
void inorder(Node root, ArrayList<Integer> ans) {
31+
if (root == null) {
32+
return;
33+
}
34+
inorder(root.left, ans);
35+
ans.add(root.data);
36+
inorder(root.right, ans);
37+
}
38+
39+
// Function to return a list containing the inorder traversal of the tree.
40+
ArrayList<Integer> inOrder(Node root) {
41+
ArrayList<Integer> ans = new ArrayList<>();
42+
inorder(root, ans);
43+
return ans;
44+
45+
}
46+
}

F-Tree/Is_Tree_Is_Correct.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
// Function to check whether a Binary Tree is BST or not.
3+
boolean isBST(Node root) {
4+
// code here.
5+
int min = Integer.MIN_VALUE;
6+
int max = Integer.MAX_VALUE;
7+
return bst(root, min, max);
8+
}
9+
public static boolean bst(Node root, int min, int max){
10+
if(root == null) return true;
11+
if(root.data <= min || root.data >= max) return false;
12+
return bst(root.left, min, root.data) && bst(root.right, root.data, max);
13+
}
14+
}

F-Tree/Kth_Large_Num.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// ----> return as.get(as.size() - K): Returns the kth largest element, which is the element at index as.size() - K in the sorted ArrayList.
2+
3+
/*
4+
3. Kth largest element in BST
5+
Difficulty: EasyAccuracy: 49.31%Submissions: 161K+Points: 2
6+
Given a Binary Search Tree. Your task is to complete the function which will return the kth largest element without doing any modification in the Binary Search Tree.
7+
8+
Examples:
9+
10+
1. Input:
11+
4
12+
/ \
13+
2 9
14+
k = 2
15+
Output: 4
16+
Explanation: 2nd Largest element in BST is 4
17+
18+
19+
2. Input:
20+
9
21+
\
22+
10
23+
k = 1
24+
Output: 10
25+
Explanation: 1st Largest element in BST is 10
26+
27+
28+
3. Input:
29+
4
30+
/ \
31+
2 9
32+
k = 3
33+
Output: 2
34+
Explanation: 3rd Largest element in BST is 2
35+
*/
36+
37+
import java.util.ArrayList;
38+
39+
import org.w3c.dom.Node;
40+
41+
class Solution {
42+
void check(Node root, ArrayList<Integer> ans) {
43+
if (root == null) {
44+
return;
45+
}
46+
check(root.left, ans);
47+
ans.add(root.data);
48+
check(root.right, ans);
49+
}
50+
51+
// return the Kth largest element in the given BST rooted at 'root'
52+
public int kthLargest(Node root, int k) {
53+
// Your code here
54+
ArrayList<Integer> ans = new ArrayList<>();
55+
check(root, ans);
56+
return ans.get(ans.size() - k);
57+
}
58+
}

F-Tree/Left_View.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Left View of Binary Tree
3+
Difficulty: EasyAccuracy: 33.74%Submissions: 532K+Points: 2
4+
You are given the root of a binary tree. Your task is to return the left view of the binary tree. The left view of a binary tree is the set of nodes visible when the tree is viewed from the left side.
5+
6+
If the tree is empty, return an empty list.
7+
8+
Examples :
9+
10+
Input: root[] = [1, 2, 3, 4, 5, N, N]
11+
12+
Output: [1, 2, 4]
13+
Explanation: From the left side of the tree, only the nodes 1, 2, and 4 are visible.
14+
15+
*/
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
21+
class Solution {
22+
void resultView(Node root, int level, List<Integer> res) {
23+
if (root == null) return;
24+
if (res.size() == level) res.add(root.data);
25+
resultView(root.left, level + 1, res);
26+
resultView(root.right, level + 1, res);
27+
}
28+
29+
List<Integer> leftView(Node root) {
30+
List<Integer> res = new ArrayList<>();
31+
resultView(root, 0, res);
32+
return res;
33+
}
34+
35+
}

F-Tree/PreOrder.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
2. Preorder Traversal
3+
4+
Given a binary tree, find its preorder traversal.
5+
6+
Examples:
7+
8+
Input:
9+
1
10+
/
11+
4
12+
/ \
13+
4 2
14+
Output: [1, 4, 4, 2]
15+
Input:
16+
6
17+
/ \
18+
3 2
19+
\ /
20+
1 2
21+
Output: [6, 3, 1, 2, 2]
22+
Input:
23+
8
24+
/ \
25+
3 10
26+
/ \ \
27+
1 6 14
28+
/ \ /
29+
4 7 13
30+
Output: [8, 3, 1, 6, 4, 7, 10, 14, 13]
31+
*/
32+
33+
import java.util.ArrayList;
34+
35+
import org.w3c.dom.Node;
36+
37+
class Solution {
38+
void PreOrder(Node root, ArrayList<Integer> ans) {
39+
if (root == null) {
40+
return;
41+
}
42+
ans.add(root.data);
43+
PreOrder(root.left, ans);
44+
PreOrder(root.right, ans);
45+
}
46+
47+
// Function to return a list containing the preorder traversal of the tree.
48+
ArrayList<Integer> preorder(Node root) {
49+
// write code here
50+
ArrayList<Integer> ans = new ArrayList<>();
51+
PreOrder(root, ans);
52+
return ans;
53+
}
54+
}

F-Tree/Right_View.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Right View of Binary Tree
3+
4+
Given a Binary Tree, Your task is to return the values visible from Right view of it.
5+
6+
Right view of a Binary Tree is set of nodes visible when tree is viewed from right side.
7+
8+
Examples :
9+
10+
Input: root = [1, 2, 3, 4, 5]
11+
2_2
12+
Output: [1, 3, 5]
13+
14+
*/
15+
16+
class Solution {
17+
// Function to return list containing elements of right view of binary tree.
18+
ArrayList<Integer> rightView(Node root) {
19+
ArrayList<Integer> list = new ArrayList<Integer>();
20+
right_view(root, list, 0);
21+
return list;
22+
}
23+
24+
private void right_view(Node root, ArrayList<Integer> list, int curr_level) {
25+
if (root == null)
26+
return;
27+
if (curr_level == list.size()) {
28+
list.add(root.data);
29+
}
30+
right_view(root.right, list, curr_level + 1);
31+
right_view(root.left, list, curr_level + 1);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)