본문 바로가기

트리5

[백준 6118] 숨바꼭질 www.acmicpc.net/problem/6118 6118번: 숨바꼭질 재서기는 수혀니와 교외 농장에서 숨바꼭질을 하고 있다. 농장에는 헛간이 많이 널려있고 재서기는 그 중에 하나에 숨어야 한다. 헛간의 개수는 N(2 2021. 1. 1.
[leetcode 236] Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).” Example 1: Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: .. 2020. 10. 31.
[leetcode 226] Invert Binary Tree Invert a binary tree. Example: Input: Output: 문제 풀이: - 재귀...난이도는 쉬움인데 해도 해도 모르겠다 재귀는 - 현재 노드에서 왼쪽 서브트리와 오른쪽 서브트리를 바꾸는 것이 중점 - 왼쪽 서브트리와 오른쪽 서브트리를 새로 만들어서 붙인다. class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root == nullptr){ return root; } TreeNode* right = invertTree(root->right); TreeNode* left = invertTree(root->left); root->left = right; root->right = left; return root; } }; 2020. 10. 30.
[leetcode 437] Path Sum III You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes). The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000. Example: root = [10,5,-3,3,2,null,11,3,.. 2020. 10. 18.