연휴도 있었고,, 이런일 저런일 때문에 잠시 소홀했다.
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class BSTIterator(object):
def __init__(self, root):
"""
:type root: Optional[TreeNode]
"""
self.stack = list()
self.pushAll(root)
def next(self):
"""
:rtype: int
"""
tmpNode = self.stack.pop()
self.pushAll(tmpNode.right)
return tmpNode.val
def pushAll(self, node):
while node is not None:
self.stack.append(node)
node = node.left
def hasNext(self):
"""
:rtype: bool
"""
return len(self.stack) > 0
# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def countNodes(self, root):
"""
:type root: Optional[TreeNode]
:rtype: int
"""
if root is None:
return 0
return self.countNodes(root.left) + self.countNodes(root.right) + 1
'일상용 > 연습장' 카테고리의 다른 글
[leetcode] 3-7회차 (0) | 2025.05.21 |
---|---|
[leetcode] 3-6회차 (0) | 2025.05.20 |
[리트코드]3-4회차 (0) | 2025.04.23 |
[리트코드] 3-3 (0) | 2025.04.22 |
[leetcode] 3-2회차 (0) | 2025.04.17 |