본문 바로가기

알고리즘 문제풀이/leetcode142

[leetcode 111] Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node with no children. Example 1: Input: root = [3,9,20,null,null,15,7] Output: 2 Example 2: Input: root = [2,null,3,null,4,null,5,null,6] Output: 5 Constraints: The number of nodes in the tree is in the range [0, 105]. .. 2020. 11. 21.
[leetcode 110] Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in which the left and right subtrees of every node differ in height by no more than 1. Example 1: Input: root = [3,9,20,null,null,15,7] Output: true Example 2: Input: root = [1,2,2,3,3,null,null,4,4] Output: false Example 3: Input: root = [] Output: true Constrain.. 2020. 11. 21.
[leetcode 107] Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree [3,9,20,null,null,15,7], return its bottom-up level order traversal as: [ [15,7], [9,20], [3] ] 문제 풀이: 레벨 기준 트리 순회 bfs로 순회하되 레벨을 기준으로 노드를 체크한다. 1. 맨 처음엔 루트 노드를 큐에 푸쉬 2. 큐의 사이즈를 체크 -> 같은 레벨에 있는 노드 수이다. 3. 벡터 생성 4. 큐의 사이즈 만.. 2020. 11. 21.
[leetcode 108] Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example: Given the sorted array: [-10,-3,0,5,9], One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced B.. 2020. 11. 20.