LintCode-二叉樹的前、中、后序遍歷-遞歸

描述

給出一棵二叉樹,返回其節(jié)點(diǎn)值的前、中、后序遍歷。

樣例

給出一棵二叉樹 {1,#,2,3},

1

2
/
3
返回 [3,2,1]

挑戰(zhàn)

你能使用非遞歸實(shí)現(xiàn)么?

代碼(遞歸)

前序遍歷

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: A Tree
    @return: Preorder in ArrayList which contains node values.
    """
    def preorderTraversal(self, root):
        # write your code here
        result = []
        if root is None:
            return result
        queue = [root]
        while queue:
            root = queue.pop()
            result.append(root.val)
            if root.right is not None:
                queue.append(root.right)
            if root.left is not None:
                queue.append(root.left)
        return result

中序遍歷
第一種寫法

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: A Tree
    @return: Inorder in ArrayList which contains node values.
    """
    def inorderTraversal(self, root):
        # write your code here
        if root is None:
            return []
        stack = [root]
        result = []
        while stack:
            new_root = stack.pop()
            if new_root.left is not None:
                stack.append(new_root)
                stack.append(new_root.left)
                continue
            else:
                result.append(new_root.val)
                while new_root.right is None:
                    try:
                        new_root = stack.pop()
                    except IndexError:
                        return result
                    result.append(new_root.val)
                stack.append(new_root.right)
        return result
                    

第二種寫法

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: A Tree
    @return: Inorder in ArrayList which contains node values.
    """
    def inorderTraversal(self, root):
        # write your code here
        if root is None:
            return []
        stack = [root]
        result = []
        while stack:
            new_root = stack[-1]
            if new_root.left is None and new_root.right is None:
                result.append(new_root.val)
                stack.pop()
                while stack:
                    new_root = stack.pop()
                    result.append(new_root.val)
                    if new_root.right is not None:
                        stack.append(new_root.right)
                        break
            else:
                if new_root.left is not None:
                    stack.append(new_root.left)
                else:
                    if new_root.right is not None:
                        stack.pop()
                        result.append(new_root.val)
                        stack.append(new_root.right)
        return result
                    

后序遍歷

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param root: A Tree
    @return: Postorder in ArrayList which contains node values.
    """
    def postorderTraversal(self, root):
        # write your code here
        result = []
        if root is None:
            return result                                       
        stack = [root]
        already = []
        while stack:
            new_root = stack.pop()
            if new_root in already:
                result.append(new_root.val)
                continue
            if new_root.left is None and new_root.right is None:
                result.append(new_root.val)
            else:
                stack.append(new_root)
                already.append(new_root)
            if new_root.right is not None:
                stack.append(new_root.right)
            if new_root.left is not None:
                stack.append(new_root.left)
        return result
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容