Today, I solved "Reorder List." Description: It is hard to understand, but we are gonna reorder the list with rule. We can easily find the rule of the form: Before: L0 -> L1 -> L2 -> L3 -> L4 After: L0 -> L4 -> L1 -> L3 -> L2 Now, how can we change the list order without changing the value? We should make the other list to re-order the list. The recommendation is seperating the list as two lists..
Today, I solved "Remove Nth Node From End of List." Description: In this problem, we are gonna delete nth node from the end of the list. For example of case 1, we found the "4" is the 2nd node from the end of the list. But how can we delete and how can we know nth node from the end? Solution: 1. First, we are gonna make two pointers on the list: left and right. 2. "right" is "n" ahead of left. F..
Today, I solved the Merge Two Sorted Lists. Description: We have two lists, and we are gonna merge those two lists. The point is that we are gonna sort new list in non-decreasing order. Solution1: We make dummy node to avoid some error. And, we make curr node to know our location in both lists. Our while loop runs until we put every values in list1 and list2. If the value of list1 is less than l..
Today, I solved the Reverse Linked List. Description: In this problem, we reverse the order of single linked list. My idea was that changing the pointer to make reverse order. So, I thought I should change like this "1->2" as a "2->1" repeatedly. In the description of the question, we can see "A linked list can be reversed either iteratively or recursively." I'm not sure about this kind of quest..
Description: This question is to know whether subRoot is in root or not. If the subRoot is in the root, return True. However, in example 2, it returns False because subRoot has unexpected child node. This Question set it is false like example 2. Solution(Recursion with DFS): Before looking at identical function, we should look the cases. In the first case, we return true when we don't have subRo..
Description: This question asked us to get 2 dimensional array which includes the value and level of tree. "3" is the only value of first level of tree. 9, 20 are the second level. 15, 7 are the third level. Each level is in the same list, we can see the different list in the output list. Solution: First, the solution used the queue method in collections library. "res" is what we are gonna retur..
This is the Leetcode question called "Binary Tree Maximum Path): Every nodes have a specific value. Some are positive and some are negative. We can start from any node of tree. When we start, we can make a direction to other node. Solution: First, we make the list name "res" to return the sum. The function "dfs" is used for recurssion and return the value of the maximum case when we go thorugh t..
Description: So, we should change the right value of the root to the left value of the root. Solution: First, we have a condition: if the tree is empty, we just return root. I made the variable name "left", "right" which is pointer the right and the left value of the root. We change the value of right to the left, the left to the right through recurrsion. Finally, return the root.
Description: In this problem, we are gonna check the identical structure and their values. Two tree must the same tree structurely. Solution: * First condition checks "both p and q are None." If it is return True. * Second condition checks "one of p and q are None." If it is, then return False. --> The reason is that just one of p or q done, that menas both tree don't have identical structure. *..
Description: We are trying to get a depth of binary tree. In example 1, weshould return "3", and example 2 should return "2." Solution1(Recursion): This is the method using recursive. We are going down to tree, adding 1 until the depth of tree. Since the recursive function have both value l and r, we should compare which is the longer. That's why we use the max(r, l) to compare which is the dept..