897. 증가 순서 찾기 트리
자신의 해법은 매우 소박한 귀속이다
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
	def __init__(self):
		self.ans = None
		self.current = None
	
	
	def increasingBST(self, root, tail = None):
		def inOrder(node):
			if node == None:
				return
			inOrder(node.left)
			if self.ans == None:
				self.ans = node
				self.current = node
			else:
				self.current.right = node
				self.current = node
			self.current.left = None
			inOrder(node.right)
		
		inOrder(root)
		return self.ans
  그러나 문제풀이에서 매우 교묘한 해법을 보고 나는 오랫동안 돌아서야 비로소 깨달았다.마크:
    def increasingBST(self, root, tail = None):
        
        # if this null node was a left child, tail is its parent
        # if this null node was a right child, tail is its parent's parent
        if not root: return tail
        # recursive call, traversing left while passing in the current node as tail
        res = self.increasingBST(root.left, root)
        # we don't want the current node to have a left child, only a single right child
        root.left = None
        # we set the current node's right child to be tail
        # what is tail? this part is important
        # if the current node is a left child, tail will be its parent
        # else if the current node is a right child, tail will be its parent's parent
        root.right = self.increasingBST(root.right, tail)
        # throughout the whole algorithm, res is the leaf of the leftmost path in the original tree
        # its the smallest node and thus will be the root of the modified tree
        return res
                이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
python 문자열 입력으로 모든 유효한 IP 주소 생성(LeetCode 93번 문제)이 문제의 공식 난이도는 Medium으로 좋아요 1296, 반대 505, 통과율 35.4%를 눌렀다.각 항목의 지표로 말하자면 보기에는 약간 규범에 맞는 것 같지만, 실제로도 확실히 그렇다.이 문제의 해법과 의도는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.