マガジンのカバー画像

競技プログラミング

145
運営しているクリエイター

#二分木

ほぼ日刊競プロ leetcode 543. Diameter of Binary Tree

ほぼ日刊競プロ leetcode 543. Diameter of Binary Tree

543. Diameter of Binary Tree
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

もっとみる
ほぼ日刊競プロ leetcode 108. Convert Sorted Array to Binary Search Tree

ほぼ日刊競プロ leetcode 108. Convert Sorted Array to Binary Search Tree

108. Convert Sorted Array to Binary Search Tree
Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
A height-balanced bina

もっとみる
ほぼ日刊競プロ leetcode 226. Invert Binary Tree

ほぼ日刊競プロ leetcode 226. Invert Binary Tree

226. Invert Binary Tree
Given the root of a binary tree, invert the tree, and return its root.

考えたこと左と右のノードを入れ替えるだけで良い.

# Definition for a binary tree node.# class TreeNode:# def __init__(self,

もっとみる
ほぼ日刊競プロ leetcode 617. Merge Two Binary Trees

ほぼ日刊競プロ leetcode 617. Merge Two Binary Trees

617. Merge Two Binary Trees
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

もっとみる
ほぼ日刊競プロ leetcode 145. Binary Tree Postorder Traversal

ほぼ日刊競プロ leetcode 145. Binary Tree Postorder Traversal

145. Binary Tree Postorder Traversal
Given the root of a binary tree, return the postorder traversal of its nodes' values.

二分木が与えられるので,値を帰りがけ法で探索し値を返せ

考えた事帰りがけ方のなので以下のように探索する必要がある.

根のノードが最後に来るように探索

もっとみる
ほぼ日刊競プロ leetcode 104. Maximum Depth of Binary Tree

ほぼ日刊競プロ leetcode 104. Maximum Depth of Binary Tree

104. Maximum Depth of Binary Tree
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

もっとみる
ほぼ日刊競プロ leetcode 938. Range Sum of BST

ほぼ日刊競プロ leetcode 938. Range Sum of BST

938. Range Sum of BST
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].

考えたこと上記のように2分木に

もっとみる
ほぼ日刊競プロ leetcode 101. Symmetric Tree

ほぼ日刊競プロ leetcode 101. Symmetric Tree

101. Symmetric Tree
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

考えたことまず初めにinorderでリストに追加していき,逆にした時と一致すれば正解なのでは?と考えた

# Definition for

もっとみる
ほぼ日刊競プロ leetcode 94. Binary Tree Inorder Traversal

ほぼ日刊競プロ leetcode 94. Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal
Given the root of a binary tree, return the inorder traversal of its nodes' values.

考えたこと二分木のinorderについて,よく知らなかったのでググる.

wikipediaより #間順・中間順・通りがけ順 (英: in-order) #もしあ

もっとみる