LeetCode 637. Average of Levels in Binary Tree

영슈·2023년 9월 8일
0

인턴십-LeetCode

목록 보기
13/20

문제 링크

https://leetcode.com/problems/average-of-levels-in-binary-tree/?envType=study-plan-v2&envId=top-interview-150

문제 해석

  • Tree 에 대한 값이 들어옴
  • null 은 없다는 뜻
  • 각 Level 의 평균 값

문제 해결

  • Depth 의 값을 함수에 같이 넘겨주자
  • 배열에 해당 Depth 에 값들을 추가하고 , 마지막에 계산

슈도 코드

inorder(node.left,height+1)
array[height].append(node.val)
inorder(node.right,height+1)

return sum(array[height])/array[height].length

결과

사담

  • 처음에 , 배열의 크기를 어떻게 가변적으로 지정해야 할지 부분에서 조금 고민했다.
  • 다른건 매우 간단한 Binary Tree 순회 문제

메모본

Writed By Obisidan

0개의 댓글