-
Notifications
You must be signed in to change notification settings - Fork 20
/
BinaryTree3.java
55 lines (45 loc) · 1.3 KB
/
BinaryTree3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class TreeNode {
int data;
TreeNode left, right;
public TreeNode(int data) {
this.data = data;
left = right = null;
}
}
class BinaryTree {
TreeNode root;
BinaryTree() {
root = null;
}
int findLevel(TreeNode node, int key, int level) {
if (node == null)
return -1; // Node not found
if (node.data == key)
return level;
int leftLevel = findLevel(node.left, key, level + 1);
if (leftLevel != -1)
return leftLevel;
int rightLevel = findLevel(node.right, key, level + 1);
return rightLevel;
}
int getLevel(int key) {
return findLevel(root, key, 0);
}
}
public class Main {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(2);
tree.root.right = new TreeNode(3);
tree.root.left.left = new TreeNode(4);
tree.root.left.right = new TreeNode(5);
int key = 5;
int level = tree.getLevel(key);
if (level != -1) {
System.out.println("Level of node with data " + key + " is: " + level);
} else {
System.out.println("Node with data " + key + " not found in the tree.");
}
}
}