close
題目:
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.
簡易版本:
中序二元樹從最小到最大。只輸出右節點
解題想法:
找到最小值,從root.right->root.left->root的順序return,去除left
code:
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def increasingBST(self, root: TreeNode) -> TreeNode: nums=[] if not root: return def MidOrder(root): if not root: return if root.left: MidOrder(root.left) nums.append(root.val) if root.right: MidOrder(root.right) MidOrder(root) Atree=TreeNode(0) cur=Atree for nums in nums: cur.right=TreeNode(nums) cur=cur.right return Atree.right
文章標籤
全站熱搜
留言列表