Skip to content

897. Increasing Order Search Tree #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tech-cow opened this issue Aug 5, 2019 · 0 comments
Closed

897. Increasing Order Search Tree #62

tech-cow opened this issue Aug 5, 2019 · 0 comments

Comments

@tech-cow
Copy link
Owner

tech-cow commented Aug 5, 2019

第一刷

最基础的解法没有实现,想的如何在每次inorder遍历的时候就更该树的结构。

有思路:每层要把root.left = None, 然后存储一个prev变量在返回的时候充当之前层数的parent指针,但实现起来写不对

看了答案,实现如下。需要重刷

class Solution(object):        
    def increasingBST(self, root):
        self.prev = None
        self.parent = None
        self.inOrder(root)
        return self.parent

    def inOrder(self, root):
        if not root: return 
        self.inOrder(root.left)
        if not self.parent:
            self.parent = root
        else:
            self.prev.right = root
        
        self.prev = root
        root.left = None
        self.inOrder(root.right)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant