Untitled
user_0781376
plain_text
3 days ago
1.8 kB
28
Indexable
import java.util.*; class Node { int data; Node left, right; // Constructor public Node(int value) { data = value; left = right = null; } } class Main{ // Function to insert a new node in the binary tree (level order insertion) public static Node insert(Node root, int data) { if (root == null) { return new Node(data); } Queue<Node> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { Node temp = queue.poll(); if (temp.left == null) { temp.left = new Node(data); break; } else { queue.add(temp.left); } if (temp.right == null) { temp.right = new Node(data); break; } else { queue.add(temp.right); } } return root; } // Function to find the maximum item quantity in the tree public static int findMax(Node root) { if (root == null) { return Integer.MIN_VALUE; // Return the smallest possible value } int leftMax = findMax(root.left);//50 int rightMax = findMax(root.right);//60 return Math.max(root.data, Math.max(leftMax, rightMax)); } public static void main(String[] args) { Node root = new Node(50); root = insert(root, 30); root = insert(root, 70); root = insert(root, 20); root = insert(root, 60); root = insert(root, 10); root = insert(root, 80); System.out.println("The maximum item quantity in the warehouse is: " + findMax(root)); } }
Editor is loading...
Leave a Comment