[BOJ] 1991. 트리 순회 (Python)
·
알고리즘/BOJ
1991번: 트리 순회첫째 줄에는 이진 트리의 노드의 개수 N(1 ≤ N ≤ 26)이 주어진다. 둘째 줄부터 N개의 줄에 걸쳐 각 노드와 그의 왼쪽 자식 노드, 오른쪽 자식 노드가 주어진다. 노드의 이름은 A부터 차례대로 알파www.acmicpc.net 난이도: Silver I문제이진 트리를 입력받아 전위 순회(preorder traversal), 중위 순회(inorder traversal), 후위 순회(postorder traversal)한 결과를 출력하는 프로그램을 작성하시오.예를 들어 위와 같은 이진 트리가 입력되면,전위 순회한 결과 : ABDCEFG // (루트) (왼쪽 자식) (오른쪽 자식)중위 순회한 결과 : DBAECFG // (왼쪽 자식) (루트) (오른쪽 자식)후위 순회한 결과 : DBE..
[LeetCode] 2467. Most Profitable Path in a Tree (Python)
·
알고리즘/LeetCode
난이도: Medium문제 설명There is an undirected tree with n nodes labeled from 0 to n - 1, rooted at node 0. You are given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. At every node i, there is a gate. You are also given an array of even integers amount, where amount[i] represents:the price needed to open the gate ..
[LeetCode] 2196. Create Binary Tree From Descriptions (Python)
·
알고리즘/LeetCode
난이도: Medium문제 설명 You are given a 2D integer array descriptions where descriptions[i] = [parenti, childi, isLefti] indicates that parenti is the parent of childi in a binary tree of unique values. Furthermore, If isLefti == 1, then childi is the left child of parenti.If isLefti == 0, then childi is the right child of parenti. Construct the binary tree described by descriptions and return its root..
[LeetCode] 1028. Recover a Tree From Preorder Traversal (Python)
·
알고리즘/LeetCode
난이도: Hard문제 설명We run a preorder depth-first search (DFS) on the root of a binary tree. At each node in this traversal, we output D dashes (where D is the depth of this node), then we output the value of this node.  If the depth of a node is D, the depth of its immediate child is D + 1.  The depth of the root node is 0. If a node has only one child, that child is guaranteed to be the left child. ..
[LeetCode] 1261. Find Elements in a Contaminated Binary Tree (Python)
·
알고리즘/LeetCode
난이도: Medium문제 설명Given a binary tree with the following rules:root.val == 0For any treeNode:If treeNode.val has a value x and treeNode.left != null, then treeNode.left.val == 2 * x + 1If treeNode.val has a value x and treeNode.right != null, then treeNode.right.val == 2 * x + 2Now the binary tree is contaminated, which means all treeNode.val have been changed to -1. Implement the FindElements cla..