Engineering Blog

Delete a Node, Is Same Tree, Move Zeroes

Delete A Node

Click to View the Orginal Question

To remove the given node,which means whoever is asking the value for this node should get the value of the next node. Plus, the next node of this removing one should be the next next node.

In diagram, with the following node list, we want to remove node 2.

1 -> 2 -> 3 -> 4

We update original Node 2 to Node 3 and skip the original Node 3, which gives out the list after removing node 2.

1 -> 3 ——> 4

Is Same Tree

Click to View the Original Question

Note: My solution is not efficient enough based on the performance, but I think it’s fairly clean.

I use recursion to solve the problem, first to define the basic cases, and recursively loop through both trees from left to right. If it hits one false, the whole result will be false.

Move Zeroes

Click to View the Original Question

Here I have create two indices one called z to index the position for zeroes, another is i to loop through the nums array. They both increment by one when they see a non-zero value, but when it is zero, z stays at position and get ready to swap the position with next non-zero value i meets. By keep doing it, it will fill a consecutive row of zeroes, till the end.

* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
var moveZeroes = function(nums) {
    var z = 0;
    for(var i = 0; i 

About Lucius Yu

Lucius is a Software Developer at LoginRadius. He was born and raised in China, but came to Edmonton to graduate from the University of Alberta, double majoring in Computer Sciences and Mathematics. Besides coding, which is his real passion, he enjoys playing ping pong and swimming.

Related Posts