Morris Traversal: 非遞迴不用棧實現對樹的中序遍曆

來源:互聯網
上載者:User

標籤:python   tree   algorithm   

參考:http://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion-and-without-stack/

<pre name="code" class="plain">1. Initialize current as root 2. While current is not NULL   If current does not have left child      a) Print current’s data      b) Go to the right child, i.e., current = current->right   Else      a) Make current as right child of the rightmost node in current's left subtree      b) Go to this left child, i.e., current = current->leftWhen we do 2.Else, for the first round it will execute 2.Else.a/b. When finish the steps and return back,we check again and reset the rightmost node in current's left subtree as null. That is the following statement:      a') Find the rightmost node in current's left subtree (its right child is current) and set its right child as null      b') Go to the right child

        cur = root        while cur is not None:            if cur.left is None:                cur = cur.right            else:                ptr = cur.left                while ptr.right != None and ptr.right != cur:                    ptr = ptr.right                if ptr.right == None:    //Else a/b, the rightmost node in current's left subtree                    ptr.right = cur                    cur = cur.left                else:                    //Else a'/b', restore the right child of the right most node in current's left subtree                    ptr.right = None                    cur = cur.right


題目舉例:

https://oj.leetcode.com/problems/recover-binary-search-tree/

https://oj.leetcode.com/problems/validate-binary-search-tree/


# Definition for a  binary tree node# class TreeNode:#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution:    # @param root, a tree node    # @return a boolean    def isValidBST(self, root):        ans = True        pre,cur = None,root        while cur is not None:            if cur.left is None:                pre = cur                cur = cur.right            else:                ptr = cur.left                while ptr.right != None and ptr.right != cur:                    ptr = ptr.right                if ptr.right == None:                    ptr.right = cur                    cur = cur.left                else:                    ptr.right = None                    pre = cur                    cur = cur.right            if pre != None and cur != None:                if pre.val >= cur.val:                    ans = False        return ans


Morris Traversal: 非遞迴不用棧實現對樹的中序遍曆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.