zoukankan      html  css  js  c++  java
  • Binary Tree的3种非Recursive遍历

    Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values.

    For example:
    Given binary tree {1,#,2,3},

       1
        
         2
        /
       3

    return [1,2,3].

    Note: Recursive solution is trivial, could you do it iteratively?

    '''
    Created on Nov 18, 2014
    
    @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
    '''
    # Definition for a  binary tree node
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        # @param root, a tree node
        # @return a list of integers
        def preorderTraversal(self, root):
            stack=[]
            vals=[]
            if(root==None): return vals
            node=root
            stack.append(node)
            while(len(stack)!=0):
                node=stack.pop()
                if(node==None): continue
                vals.append(node.val)
                stack.append(node.right)
                stack.append(node.left)
            
            return vals
            
    View Code

    Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values.

    For example:
    Given binary tree {1,#,2,3},

       1
        
         2
        /
       3

    return [1,3,2].

    Note: Recursive solution is trivial, could you do it iteratively?

    confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

    '''
    Created on Nov 18, 2014
    
    @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
    '''
    # Definition for a  binary tree node
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        # @param root, a tree node
        # @return a list of integers
        def inorderTraversal(self, root):
            stack=[]
            vals=[]
            visited={}
            if(root==None): return vals
            node=root
            stack.append(node)
            visited[node]=1
            while(len(stack)!=0):
                if(node.left!=None and visited.has_key(node.left)==False):
                    node=node.left 
                    stack.append(node)
                    visited[node]=1
                else:
                    node=stack.pop()
                    if(node==None): continue
                    vals.append(node.val)
                    if(node.right!=None):
                        stack.append(node.right)
                        node=node.right
            return vals
      
    View Code

    Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values.

    For example:
    Given binary tree {1,#,2,3},

       1
        
         2
        /
       3

    return [3,2,1].

    Note: Recursive solution is trivial, could you do it iteratively?

    '''
    Created on Nov 19, 2014
    
    @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
    '''
    # Definition for a  binary tree node
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        # @param root, a tree node
        # @return a list of integers
        def postorderTraversal(self, root):
            visited={}
            stack=[]
            vals=[]
            if(root==None): return vals
            node=root
            
            stack.append(node)
            visited[node]=1
            
            while(len(stack)!=0):
                node=stack[-1]
                if(node.left !=None and visited.has_key(node.left)==False):
                    stack.append(node.left)
                    visited[node.left]=1
                    continue
                else:      
                    if(node.right!=None and visited.has_key(node.right)==False):
                        stack.append(node.right)
                        visited[node.right]=1
                        continue
                node=stack.pop()
                if(node==None): continue
                vals.append(node.val)
    
            return vals
            
    View Code
  • 相关阅读:
    Crowdsec:一款面向Linux的现代化协作式大型多人防火墙
    Linux文件操作:利用C语言删除某个目录下的文件
    GitHub 长期被中国人“霸榜”?看完这些榜单,这又是什么新姿势?
    英语不好学不好编程?程序员记忆单词专属诀窍,效果简直要逆天
    谁说女生不适合当程序员?世界第一位程序员,就是女性!
    C语言丨一篇文章带你认识递归与迭代
    C语言进阶丨(七)数组的基本概念和初始化【1】
    赔97.6万元!腾讯一程序员违反竞业协议,三年白干了!
    雪夜思绪
    算法复习(2)递归
  • 原文地址:https://www.cnblogs.com/scottgu/p/4108227.html
Copyright © 2011-2022 走看看