diff --git a/Java/InsertionInBST.java b/Java/InsertionInBST.java new file mode 100644 index 0000000..bda5d50 --- /dev/null +++ b/Java/InsertionInBST.java @@ -0,0 +1,69 @@ +// A class to store a BST node +class Node +{ + int data; + Node left, right; + + // Function to create a new binary tree node having a given key + Node(int key) + { + data = key; + left = right = null; + } +} + +class Main +{ + // Function to perform inorder traversal on the tree + public static void inorder(Node root) + { + if (root == null) { + return; + } + + inorder(root.left); + System.out.print(root.data + " "); + inorder(root.right); + } + + // Recursive function to insert a key into a BST + public static Node insert(Node root, int key) + { + // if the root is null, create a new node and return it + if (root == null) { + return new Node(key); + } + + // if the given key is less than the root node, + // recur for the left subtree + if (key < root.data) { + root.left = insert(root.left, key); + } + + // otherwise, recur for the right subtree + else { + // key >= root.data + root.right = insert(root.right, key); + } + + return root; + } + + // Function to construct a BST from given keys + public static Node constructBST(int[] keys) + { + Node root = null; + for (int key: keys) { + root = insert(root, key); + } + return root; + } + + public static void main(String[] args) + { + int[] keys = { 15, 10, 20, 8, 12, 16, 25 }; + + Node root = constructBST(keys); + inorder(root); + } +} diff --git a/Java/mergeTwoSortedLists.java b/Java/mergeTwoSortedLists.java new file mode 100644 index 0000000..fb67d79 --- /dev/null +++ b/Java/mergeTwoSortedLists.java @@ -0,0 +1,32 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode dummy=new ListNode(); + ListNode tail=dummy; + while(list1!=null && list2!=null){ + if(list1.val