Algorithm
February LeetCoding Challenge 2021
Trim a Binary Search Tree Solution
Given the root
of a binary search tree and the lowest and highest boundaries as low
and high
, trim the tree so that all its elements lies in [low, high]
. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node’s descendant should remain a descendant). It can be proven that there is a unique answer.
Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.
Example 1:
Input: root = [1,0,2], low = 1, high = 2
Output: [1,null,2]
Example 2:
Input: root = [3,0,4,null,2,null,null,1], low = 1, high = 3
Output: [3,2,null,1]
Example 3:
Input: root = [1], low = 1, high = 2
Output: [1]
Example 4:
Input: root = [1,null,2], low = 1, high = 3
Output: [1,null,2]
Example 5:
Input: root = [1,null,2], low = 2, high = 4
Output: [2]
Constraints:
- The number of nodes in the tree in the range
[1, 10]
. 0 <= Node.val <= 10
- The value of each node in the tree is unique.
root
is guaranteed to be a valid binary search tree.0 <= low <= high <= 10
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} low
* @param {number} high
* @return {TreeNode}
*/
var trimBST = function(root, low, high) {
if (root == null)
return null;
if (root.val > high) //current node>upper limit
return trimBST(root.left, low, high); //recursively call function on left node
if (root.val < low) //current node<upper limit
return trimBST(root.right, low, high);//recursively call function on right node
root.left = trimBST(root.left, low, high);//current node within range, update left link by trimming left subtree results
root.right = trimBST(root.right, low, high);
return root;
};
Review
Learned that hackers often utilize injection flaws to obtain administator previlieges and attack systems. _A better way to prevent this is to limit the use of kernel-level directives and use more library functions in quering database.Various Form of Injections
Injection Flaws
OS Command Injection
SQL Injection
LDAP InjectionInjection Vulnerabilities in Different Databases
MSSQL injection
Oracle injection
DB2 injection
Postgres injection
MySQL injectionTip
3 Common Error Types
SyntaxError
Code that cannot be interpreted by the compiler. Make sure you properly opened and closed all brackets, braces, and parentheses and that you didn’t include any invalid semicolons.
ReferenceError
Using a variable that does not exist. Make sure all variables are properly declared.
TypeError
Performing an operation on a value of the wrong type. For example, we tried to use a string method on a number.
Error Types DescriptionDebugging Review
- Is your code throwing errors? If so, read the error stack trace for the type, description, and location of the error. Go to the error’s location and try to fix.
- Is your code broken but not throwing errors? Walk through your code using
console.log()
statements. When unexpected results occur, isolate the bug and try to fix it. - Did you locate the bug using steps 1 and 2, but can’t fix the bug? Consult documentation to make sure you are using all JavaScript functionality properly. If you are still stuck, Google your issue and consult Stack Overflow for help. Read solutions or post your own Stack Overflow question if none exist on the topic.
Pair-Programming
Try pair programmingJavaScript Syntax: Arrays, Loops, Objects, Iterators
//reverseArray function
const sentence = ['sense.','make', 'all', 'will', 'This'];
function reverseArray(sentence){
let newArray=[];
for(let i=sentence.length-1;i>=0;i--){
newArray.push(sentence[i]);
}
return newArray;
};
console.log(reverseArray(sentence));
Share
Software engineer’s key skill
Job requirements in different locations of Australia
Jobs Hub Assistance from Australia Gov
Technologies Career Advice
FreeCodeCamp, Front-end problem sets learning & free certificaiton
CodeWar, similar to LeetCode, but much closer to practical useSoft Skills in IT
Communication
- Keeping your verbal and written communication clear, concise and confident.
- Showing that you understand your audience and can tailor your communication to them.
- Listening to and considering the views of others.
-
Planning and organization
Showing that you can structure a task or project.
- Supply examples of past projects that you’ve successfully managed.
- Explaining how you manage an activity and allocate time to individual tasks.
Describing how you anticipate and address challenges.
Drive, motivation and enthusiasm
Showing you have the determination to achieve a desired result.
- Proving that you can maintain your optimism and enthusiasm even when things get tough.
- Providing evidence that you can bounce back from any setbacks.
Choosing jobs that excite you.
Problem-solving
Approaching problems in a logical, creative and goal-oriented way.
- Showcasing your ability to view problems from a number of angles.
Providing that you can anticipate potential pitfalls and prevent them happening.
Teamwork
Showing that you can build and maintain positive working relationships.
- Describing how you share information, support colleagues and show respect for alternative views.
- Discussing how you have kept projects on track to by working tactfully and co-operatively with others.