|
| 1 | +//{ Driver Code Starts |
| 2 | +// Initial Template for Java |
| 3 | + |
| 4 | +import java.io.*; |
| 5 | +import java.util.*; |
| 6 | +import java.util.LinkedList; |
| 7 | +import java.util.Queue; |
| 8 | + |
| 9 | +class Node { |
| 10 | + int data; |
| 11 | + Node left; |
| 12 | + Node right; |
| 13 | + |
| 14 | + Node(int data) { |
| 15 | + this.data = data; |
| 16 | + left = null; |
| 17 | + right = null; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +class GfG { |
| 22 | + |
| 23 | + static Node buildTree(String str) { |
| 24 | + |
| 25 | + if (str.length() == 0 || str.charAt(0) == 'N') { |
| 26 | + return null; |
| 27 | + } |
| 28 | + |
| 29 | + String ip[] = str.split(" "); |
| 30 | + // Create the root of the tree |
| 31 | + Node root = new Node(Integer.parseInt(ip[0])); |
| 32 | + // Push the root to the queue |
| 33 | + |
| 34 | + Queue<Node> queue = new LinkedList<>(); |
| 35 | + |
| 36 | + queue.add(root); |
| 37 | + // Starting from the second element |
| 38 | + |
| 39 | + int i = 1; |
| 40 | + while (queue.size() > 0 && i < ip.length) { |
| 41 | + |
| 42 | + // Get and remove the front of the queue |
| 43 | + Node currNode = queue.peek(); |
| 44 | + queue.remove(); |
| 45 | + |
| 46 | + // Get the current node's value from the string |
| 47 | + String currVal = ip[i]; |
| 48 | + |
| 49 | + // If the left child is not null |
| 50 | + if (!currVal.equals("N")) { |
| 51 | + |
| 52 | + // Create the left child for the current node |
| 53 | + currNode.left = new Node(Integer.parseInt(currVal)); |
| 54 | + // Push it to the queue |
| 55 | + queue.add(currNode.left); |
| 56 | + } |
| 57 | + |
| 58 | + // For the right child |
| 59 | + i++; |
| 60 | + if (i >= ip.length) break; |
| 61 | + |
| 62 | + currVal = ip[i]; |
| 63 | + |
| 64 | + // If the right child is not null |
| 65 | + if (!currVal.equals("N")) { |
| 66 | + |
| 67 | + // Create the right child for the current node |
| 68 | + currNode.right = new Node(Integer.parseInt(currVal)); |
| 69 | + |
| 70 | + // Push it to the queue |
| 71 | + queue.add(currNode.right); |
| 72 | + } |
| 73 | + i++; |
| 74 | + } |
| 75 | + |
| 76 | + return root; |
| 77 | + } |
| 78 | + |
| 79 | + static void printInorder(Node root) { |
| 80 | + if (root == null) return; |
| 81 | + |
| 82 | + printInorder(root.left); |
| 83 | + System.out.print(root.data + " "); |
| 84 | + |
| 85 | + printInorder(root.right); |
| 86 | + } |
| 87 | + |
| 88 | + public static void main(String[] args) throws IOException { |
| 89 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 90 | + |
| 91 | + int t = Integer.parseInt(br.readLine()); |
| 92 | + |
| 93 | + while (t > 0) { |
| 94 | + String s = br.readLine(); |
| 95 | + Node root = buildTree(s); |
| 96 | + Solution g = new Solution(); |
| 97 | + System.out.println(g.minValue(root)); |
| 98 | + t--; |
| 99 | + |
| 100 | +System.out.println("~"); |
| 101 | +} |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// } Driver Code Ends |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +/* |
| 111 | +class Node { |
| 112 | + int data; |
| 113 | + Node left; |
| 114 | + Node right; |
| 115 | + Node(int data) { |
| 116 | + this.data = data; |
| 117 | + left = null; |
| 118 | + right = null; |
| 119 | + } |
| 120 | +} |
| 121 | +*/ |
| 122 | +class MinElementOfBST { |
| 123 | + // Function to find the minimum element in the given BST. |
| 124 | + int minValue(Node root) { |
| 125 | + |
| 126 | + if(root == null){ |
| 127 | + return -1; |
| 128 | + } |
| 129 | + |
| 130 | + return postorder(root); |
| 131 | + } |
| 132 | + public static int postorder(Node root){ |
| 133 | + if(root == null){ |
| 134 | + return Integer.MAX_VALUE; |
| 135 | + } |
| 136 | + |
| 137 | + int leftMin = postorder(root.left); |
| 138 | + int rightMin = postorder(root.right); |
| 139 | + return Math.min(root.data, Math.min(leftMin, rightMin)); |
| 140 | + } |
| 141 | +} |
0 commit comments