본문 바로가기

tree14

[LEET CODE] 110. Balanced Binary Tree # Problem Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: Constraints: The number of nodes in the tree is in the range [0, 5000]. -104 1: return False return a and b # 서브 트리도 모두 균형인지 확인 # Solution 1 - 재귀 구조로 높이 차이 계산 class Solution: def isBalanced(self, root: TreeNode) -> bool: def check(root): if not root: return 0 left = c.. 2021. 4. 20.
[LEET CODE] 617. Merge Two Binary Trees # Problem 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 no.. 2021. 4. 20.
❗️[Baekjoon] 2250. 트리의 높이와 너비 # Problem 이진트리를 다음의 규칙에 따라 행과 열에 번호가 붙어있는 격자 모양의 틀 속에 그리려고 한다. 이때 다음의 규칙에 따라 그리려고 한다. 이진트리에서 같은 레벨(level)에 있는 노드는 같은 행에 위치한다. 한 열에는 한 노드만 존재한다. 임의의 노드의 왼쪽 부트리(left subtree)에 있는 노드들은 해당 노드보다 왼쪽의 열에 위치하고, 오른쪽 부트리(right subtree)에 있는 노드들은 해당 노드보다 오른쪽의 열에 위치한다. 노드가 배치된 가장 왼쪽 열과 오른쪽 열 사이엔 아무 노드도 없이 비어있는 열은 없다. 이와 같은 규칙에 따라 이진트리를 그릴 때 각 레벨의 너비는 그 레벨에 할당된 노드 중 가장 오른쪽에 위치한 노드의 열 번호에서 가장 왼쪽에 위치한 노드의 열 번호를.. 2021. 4. 20.
[LEET CODE] 226. Invert Binary Tree # Problem Given the root of a binary tree, invert the tree, and return its root. Constraints: The number of nodes in the tree is in the range [0, 100]. -100 TreeNode: if root: root.left, root.right = \ self.invertTree(root.right), self.invertTree(root.left) return root return None return None은 생략가능 ( 파이썬은 자동으로 None 리턴 ) # Solution 2 - 반복구조로 BFS class Solution: def invertTree(self, root: TreeNode.. 2021. 4. 20.
❗️[LEET CODE] 687. Longest Univalue Path # Problem Given the root of a binary tree, return the length of the longest path, where each node in the path has the same value. This path may or may not pass through the root. The length of the path between two nodes is represented by the number of edges between them. Constraints: The number of nodes in the tree is in the range [0, 104]. -1000 2021. 4. 20.
[LEET CODE] 543. Diameter of Binary Tree # Problem 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. Constraints: The number of nodes in the tree is in the range [1, 104]. -.. 2021. 4. 19.
반응형