Showing posts with label Largest smaller BST key. Show all posts
Showing posts with label Largest smaller BST key. Show all posts

Saturday, January 13, 2018

JavaScript: Largest smaller binary search tree key

January 13, 2018

Introduction


I do not have time to write a lot of JavaScript code last 12 months, since most of time I have to write C# code. But this morning 12:00 pm I had a mock interview, I met a programmer and then he showed me how to solve the problem using JavaScript.

Code review


Here is JavaScript code with his code from line 26 to 59. The peer came out the recursive solution and then base case correctly, and only comment I had is to clean up code, avoid duplicate node.right on line 46 and 51.

Since the peer solves the problem in 20 minutes, I told him that I will start my algorithm. After I complete the algorithm, I will ask him an extra algorithm problem.

Thursday, June 1, 2017

Leetcode 230: Kth Smallest Element in a BST

June 1, 2017

Plan to work on the problem called "Kth Smallest Element in a BST". The problem statement link is here.

Plan to study the algorithm "Second largest element in BST". The link is here.

Introduction


One of most common algorithms is to find the largest value in a set. To solve the problem, Julia has to learn to step back, to find a value in the set first, and then compare to existing largest value when a new value is found.

Julia learned the lesson through the mock experience. The algorithm is to find largest smaller value than the given value in Binary Search Tree (BST). In other words, any number in the BST less than the given value can be added to the set, and the task is to find the maximum value.

The lesson learned is to find a smaller value first, and then save it as maximum value; continue to search and continue to compare with existing max value.

Algorithm study