Created
June 23, 2020 13:20
-
-
Save jsrimr/70920b62bb090124589a0d8d44918a75 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def count_complete_tree(): | |
| h = dict() | |
| def backtrack(node): #-> return node 의 높이. 밑에 몇개 있냐. | |
| if not node.left and not node.right: | |
| h[node.val] = 0 | |
| return h[node.val] | |
| left = backtrack(node.left) +1 if node.left else 0 | |
| right = backtrack(node.right) + 1 if node.right else 0 | |
| h[node.val] = left if left >= right else 0 | |
| return h[node.val] | |
| if root: | |
| backtrack(root) | |
| else: | |
| return 0 | |
| def is_complete(node): | |
| left = h[node.left.val] if node.left else 0 | |
| right =h[node.right.val] if node.right else 0 | |
| if left >= right: | |
| return True | |
| stack = deque([root]) | |
| cnt = 0 | |
| while stack: | |
| node = stack.pop() | |
| if is_complete(node): | |
| cnt +=1 | |
| if node.right: stack.append(node.right) | |
| if node.left : stack.append(node.left) | |
| return cnt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment