본문 바로가기

leetcode/tree

(4)
[python][leetcode 129]. Sum Root to Leaf Numbers -- dfs,recur You are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number. For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123. Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer. A leaf node is a node with no children. 한줄해석. 좌측과 같은 tree가 있을때, lea..
[python] 34. Find First and Last Position of Element in Sorted Array 더보기 Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4] Example 2: Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1] Example 3: Input:..
[leetcode][c++] 199. Binary Tree Right Side View 문제 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. 한줄요약 맨 오른쪽 노드 출력하기 왼쪽 output : [1,3,4] 오른쪽 output : [ 1,3,3,7] 문제 Point tree를 어떻게 순회 할 것인가 ? (오른쪽 예시를 보면 갑자기 난이도 급상승) 생각하며보면, 왼쪽에서 오른쪽으로 각 노드들을 방문해 가면서, 각 단계에서 가장 오른쪽 노드(=마지막노드)를 저장하여 반환한다. 오른쪽 그림에서 맨 마지막 단계는 7 밖에 없으므로 7을 저장한다. 풀이 : 1) 왼쪽에서..
[c++][LeetCode Curated Algo 170][314. Binary Tree Vertical Order Traversal] 문제 Given the root of a binary tree, return the vertical order traversal of its nodes' values. (i.e., from top to bottom, column by column). If two nodes are in the same row and column, the order should be from left to right. Input: root = [3,9,8,4,0,1,7,null,null,null,2,5] Output: [[4],[9,5],[3,0,1],[8,2],[7]] Input: root = [3,9,20,null,null,15,7] Output: [[9],[3,15],[20],[7]] 한줄해석 위와 같은 tree 구조..