본문 바로가기

dfs18

[Leetcode 98] - Validate Binary Search w/ Python Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: ● The left subtree of a node contains only nodes with keys less than the node's key. ● The right subtree of a node contains only nodes with keys greater than the node's key. ● Both the left and right subtrees must also be binary search trees. 이진 트리가 주어졌을때 이진 탐색 트리인지 확인하는 문제 각 .. 2024. 4. 10.
[Leetcode 1448] - Count Good Nodes in Binary Tree w/ Python Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X. Return the number of good nodes in the binary tree. 이진 트리가 주어졌을때 다음으로 탐색할 노드의 값이 현재 위치한 노드의 값보다 큰 숫자를 파악해서 리턴 ※ Root 노드는 언제나 포함된다 https://leetcode.com/problems/count-good-nodes-in-binary-tree/description/ Example 1: Input: root = [3,1,4,3,null,1,5] Output: 4 .. 2024. 4. 10.
[Leetcode 199] - Binary Tree Right Side View w/ Python Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. 이진 트리 우측, 조금 거리가 있는곳에서 바라본다고 생각해보자 우측에서 보이는 노드를 위에서 아래로 배열에 저장해서 리턴 https://leetcode.com/problems/binary-tree-right-side-view/description/ Example 1: Input: root = [1,2,3,null,5,null,4] Output: [1,3,4] Example 2: Input: root = [1,null,3] Outpu.. 2024. 4. 10.
[Leetcode 102] - Binary Tree Level Order Traversal w/ Python Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level). 이진 트리가 주어졌을때 각 레벨 순서 순회의 값을 찾아서 리턴하는 문제 https://leetcode.com/problems/binary-tree-level-order-traversal/description/ Example 1: Input: root = [3,9,20,null,null,15,7] Output: [[3],[9,20],[15,7]] Example 2: Input: root = [1] Output: [[1]] Example 3: Input: root = [] O.. 2024. 4. 9.
[Leetcode 572] - Subtree of Another Tree Given the roots of two binary trees root and subRoot, return true if there is a subtree of root with the same structure and node values of subRoot and false otherwise. A subtree of a binary tree tree is a tree that consists of a node in tree and all of this node's descendants. The tree tree could also be considered as a subtree of itself. root, subRoot라는 두개의 이진 트리가 주어졌을때 root 안에 똑같은 subroot가 포함되.. 2024. 4. 9.
[Leetcode 100] - Same Tree w/ Python Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. p, q이라는 이진트리 두개가 주어졌을때 둘이 같은지 확인하는 문제 모양, 값다 같은지 확인한 후 True/False를 리턴하면 된다 https://leetcode.com/problems/same-tree/description/ Example 1: Input: p = [1,2,3], q = [1,2,3] Output: true Ex.. 2024. 4. 9.
[Leetcode 110] - Balanced Binary Tree w/ Python Given a binary tree, determine if it is height-balanced 이진 트리가 주어졌을때 균형 이진 트리인지 확인해야 되는 문제 https://leetcode.com/problems/balanced-binary-tree/description/ 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 함수 아웃풋: [ 현재까지 탐색한 부분이 균형인가 확인 , 현재까지 탐색한 길이 확인] 1. DFS를 통해 Root 노드가 None일때.. 2024. 4. 9.
[Leetcode 543] - Diameter of Binary Tree w/ Python Given the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. The length of a path between two nodes is represented by the number of edges between them. 이진 트리가 주어졌을때 노드를 통해 가장 긴 거리를 계산해야 되는 문제 루트를 꼭 통과 안해도 되는 전제하에 노드가 2개 있을시 거리는 1로 계산한.. 2024. 4. 9.
[Leetcode 104] - Maximum Depth of Binary Tree w/ Python Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 이진 트리가 주어졌을때 깊이를 찾아내라는 문제 https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3 Example 2: Input: root = [1,null,2] Output: 2 트리에서 깊이를 .. 2024. 4. 8.
반응형