Coding Test/LeetCode
-
[LeetCode] 897. Increasing Order Search Tree c++Coding Test/LeetCode 2022. 9. 27. 12:49
문제 Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child. 이진탐색트리의 값들을 node.right로 오름차순으로 정렬하는 문제이다. 주어진 이진탐색트리들은 node.left → node.val → node.right 순으로 값들이 오름차순으로 정렬되어 있다. 문제 해결 1. root노드의 left 먼저 탐색 2. 최하위 까지 탐색했다면 ans노드의 right에 root값을 저장 3. ans노드를 right로 ..
-
[LeetCode] 617. Merge Two Binary Trees c++Coding Test/LeetCode 2022. 9. 27. 11:10
문제 You are given two binary trees root1 and root2. Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of t..
-
[LeetCode] 2331. Evaluate Boolean Binary Tree c++Coding Test/LeetCode 2022. 9. 26. 21:43
문제 You are given the root of a full binary tree with the following properties: Leaf nodes have either the value 0 or 1, where 0 represents False and 1 represents True. Non-leaf nodes have either the value 2 or 3, where 2 represents the boolean OR and 3 represents the boolean AND. The evaluation of a node is as follows: If the node is a leaf node, the evaluation is the value of the node, i.e. Tru..
-
[LeetCode] 938. Range Sum of BST c++Coding Test/LeetCode 2022. 9. 26. 18:11
문제 Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high]. 이진탐색트리의 node중에 low와 high사이에 있는 값을 모두 더한 값을 구하는 문제이다. 문제 해결 class Solution { public: int nSum = 0; int rangeSumBST(TreeNode* root, int low, int high) { if(root == NULL) return nSum; if(root->val >= low && root-> val val; } rangeSumBST..
-
[LeetCode] 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree c++Coding Test/LeetCode 2022. 9. 26. 15:17
문제 Given two binary trees original and cloned and given a reference to a node target in the original tree. The cloned tree is a copy of the original tree. Return a reference to the same node in the cloned tree. Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree. original 트리에 target노드를 찾는 문제이다. 문제 해결 Tree..
-
[LeetCode] 27. Remove Element c++Coding Test/LeetCode 2022. 9. 15. 13:08
문제 설명 Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k el..
-
[LeetCode] 26. Remove Duplicates from Sorted Array c++Coding Test/LeetCode 2022. 9. 15. 10:28
문제 설명 Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elemen..