From c94b71d9b2f12868ce3907632e6e47acbd87a9a1 Mon Sep 17 00:00:00 2001 From: Pratyush Joshi <86593014+PratyushJoshi@users.noreply.github.com> Date: Sun, 22 Oct 2023 00:32:50 +0530 Subject: [PATCH] Update BST Binary Search Tree.java In the leftmostSum, rightmostSum, and lastNodeSum methods, you're calculating the sums correctly, but the results are not being used or returned properly. You need to return the calculated sums from these methods. The sameParent method is declared as an instance method, but it's used as if it's a static method in the comment. Depending on how you intend to use it, you should declare it as a static method or adjust its usage accordingly. In the preOrderBST and bstFromPreorder methods, you should consider changing the parameter type to long[] to match the data type you're working with (i.e., long) in your TreeNode. There is a missing import statement for BigInteger. You should add the following import statement at the beginning of your code: --- Arrays/BST Binary Search Tree.java | 38 +++++++++++++++--------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Arrays/BST Binary Search Tree.java b/Arrays/BST Binary Search Tree.java index 4069c8d..4c1cf83 100644 --- a/Arrays/BST Binary Search Tree.java +++ b/Arrays/BST Binary Search Tree.java @@ -1,18 +1,18 @@ -class TreeNode{ +import java.math.BigInteger; +import java.util.Scanner; + +class TreeNode { TreeNode left, right; - int leftSize =0, rightSize=0; - long data, height = 0; - public TreeNode(long data){ - this.data = data; - this.height = 1; - } - public TreeNode(long data, TreeNode left, TreeNode right){ + int leftSize = 0, rightSize = 0; + long data, height = 1; + + public TreeNode(long data) { this.data = data; - this.left = left; - this.right = right; } } -class BinaryTree { + +public class BinaryTree { + public static TreeNode createTree() { TreeNode root = null; Scanner sc = new Scanner(System.in); @@ -63,7 +63,7 @@ public static TreeNode formTree(long[] nums, int low, int high) { return null; } int mid = (high + low) / 2; - TreeNode node = new TreeNode((int) nums[mid]); + TreeNode node = new TreeNode(nums[mid]); node.left = formTree(nums, low, mid - 1); node.right = formTree(nums, mid + 1, high); return node; @@ -179,10 +179,10 @@ public static long leftmostSum(TreeNode root) { if (root != null) { if (root.left != null) { sum += root.data; - leftmostSum(root.left); + sum += leftmostSum(root.left); } else if (root.right != null) { sum += root.data; - leftmostSum(root.right); + sum += leftmostSum(root.right); } } return sum; @@ -192,10 +192,10 @@ public static long rightmostSum(TreeNode root) { long sum = 0; if (root != null) { if (root.right != null) { - rightmostSum(root.right); + sum += rightmostSum(root.right); sum += root.data; } else if (root.left != null) { - rightmostSum(root.left); + sum += rightmostSum(root.left); sum += root.data; } } @@ -214,11 +214,11 @@ public static long lastNodeSum(TreeNode root) { return sum; } - boolean sameParent(TreeNode node, TreeNode a, TreeNode b) { + public boolean sameParent(TreeNode node, TreeNode a, TreeNode b) { if (node == null) return false; return ((node.left == a && node.right == b) || (node.left == b && node.right == a) || sameParent(node.left, a, b) || sameParent(node.right, a, b)); - } // then return true, if not then return false + } int treeLevel(TreeNode root, TreeNode node, int level) { if (root == null) @@ -247,7 +247,7 @@ public static TreeNode postOrderArrayToBST(int[] arr) { return root; } - TreeNode findLCA (TreeNode root,int node1, int node2){ + TreeNode findLCA(TreeNode root, int node1, int node2) { if (root == null) return root; if (root.data == node1 || root.data == node2) return root;