본문 바로가기
Problem Solving/Codility

[Python] TreeHeight

by 부르르 2021. 7. 18.

https://app.codility.com/demo/results/trainingMGZBGQ-R79/

 

Test results - Codility

In this problem we consider binary trees, represented by pointer data structures. A binary tree is either an empty tree or a node (called the root) consisting of a single integer value and two further binary trees, called the left subtree and the right sub

app.codility.com


Point

  • dfs를 이용해서 height(depth)를 카운트한다

Tip

  • 탈출 조건을 잘 분기해준다
  • 탈출 조건의 리턴값을 잘 핸들링해준다

Task Score

100%

Correctness

100%

Performance

Not assessed

Detected time complexity

 

from extratypes import Tree  # library with types used in the task

def dfs(tree, height):
    left_height = right_height = 0

    if tree.l:
        left_height = dfs(tree.l,height+1)
    if tree.r:
        right_height = dfs(tree.r,height+1)
    
    if tree.l or tree.r:
        return max(left_height,right_height)
    else:
        return height

def solution(T):
    return dfs(T,0)

 

문제를 푸는데 도움이 된 테스트 케이스

 

 

728x90
반응형

'Problem Solving > Codility' 카테고리의 다른 글

[Python] LongestPassword  (0) 2021.07.18
[Python] StrSymmetryPoint  (0) 2021.07.18
[Python] FirstUnique  (0) 2021.07.15
[Python] DisappearingPairs  (0) 2021.07.15
[Python] 8.1. Dominator  (0) 2021.04.05

댓글