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:
Week 7 - 图1
Input: root = [1,0,2], low = 1, high = 2
Output: [1,null,2]

Example 2:
Week 7 - 图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
    1. /**
    2. * Definition for a binary tree node.
    3. * function TreeNode(val, left, right) {
    4. * this.val = (val===undefined ? 0 : val)
    5. * this.left = (left===undefined ? null : left)
    6. * this.right = (right===undefined ? null : right)
    7. * }
    8. */
    9. /**
    10. * @param {TreeNode} root
    11. * @param {number} low
    12. * @param {number} high
    13. * @return {TreeNode}
    14. */
    15. var trimBST = function(root, low, high) {
    16. if (root == null)
    17. return null;
    18. if (root.val > high) //current node>upper limit
    19. return trimBST(root.left, low, high); //recursively call function on left node
    20. if (root.val < low) //current node<upper limit
    21. return trimBST(root.right, low, high);//recursively call function on right node
    22. root.left = trimBST(root.left, low, high);//current node within range, update left link by trimming left subtree results
    23. root.right = trimBST(root.right, low, high);
    24. return root;
    25. };
    image.png

    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 Injection

    Injection Vulnerabilities in Different Databases

    MSSQL injection
    Oracle injection
    DB2 injection
    Postgres injection
    MySQL injection

    Tip

    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 Description

    Debugging Review

  1. 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.
  2. 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.
  3. 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 programming

    JavaScript Syntax: Arrays, Loops, Objects, Iterators

    1. //reverseArray function
    2. const sentence = ['sense.','make', 'all', 'will', 'This'];
    3. function reverseArray(sentence){
    4. let newArray=[];
    5. for(let i=sentence.length-1;i>=0;i--){
    6. newArray.push(sentence[i]);
    7. }
    8. return newArray;
    9. };
    10. 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 use

    Soft 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.
  • Thinking before you speak.

    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.

Introduction to Fintech
Engineers Australia
ACS