zoukankan      html  css  js  c++  java
  • Leetcode: 二叉树的后序遍历

    Leetcode: 二叉树的后序遍历


    后遍历的流程:一直往下走,把路口记下来,假如路口有馒头,我们是不吃的,因为要先吃最后的,中间的岔路如果有岔路,就把右边的岔路记下来,但是因为右边没有走过,所以访问的时候也不能吃,但是左边的路因为一直往下走,所以左边没有路了,再次回头左边那些路时,那些馒头就可以吃了。

    题目:
    给定一个二叉树,返回它的后序遍历。

    输入: [1,null,2,3]  
       1
        
         2
        /
       3 
    
    输出: [3,2,1]
    

    Python 实现

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    class Stack:
        def __init__(self,x,status):
            self.node = x
            self.p = status
            
    class Solution(object):
        def postorderTraversal(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            result = []
            stack = []
            flag = 1
            while True:
                while root and flag:
                    stack.append(Stack(root,0))
                    if root.right:
                        stack.append(Stack(root.right,1)) #右边的节点还没完全遍历完,所以不能访问。
                    root = root.left
                if len(stack) == 0:
                    return result
                tmp = stack.pop()
                root = tmp.node
                flag = tmp.p
                if flag == 1: #这个节点不能访问,需要访问完子节点才能访问
                    continue
                result.append(root.val)
                
                
    

    C语言实现

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    /**
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    struct Stack{
        struct TreeNode *node;
        int p;
    };
    int* postorderTraversal(struct TreeNode* root, int* returnSize) {
        int *result = (int *)malloc(sizeof(int) * 100);
        int i = 0;
        struct Stack stack[100]; //模拟栈;
        
        int top = -1; //栈顶
        int flag = 1;
        while(true){
            while(flag == 1 && root){
                stack[++top].node = root;
                stack[top].p = 0; //这个节点可以直接访问;
                if(root->right){
                stack[++top].node = root->right;
                stack[top].p = 1; // 右节点就不能直接被访问;
                }
                root = root->left;
            }
            if(top == -1){
                *returnSize = i;
                return result;
            }
            root = stack[top].node; //访问栈顶元素,
            flag = stack[top--].p;
            if(flag == 1)
                continue; // 这时候还不能被访问的,
            result[i++] = root->val;     
        }
        
    }
    
  • 相关阅读:
    HDU 2602
    ZOJ 1074 最大子矩阵和
    POJ 3903
    最大子段和
    HDU 1052
    CodeForces 18C
    CodeForces 546B-Soldier and Badges
    UVA 11462-Age sort
    Codeforces Round #326 (Div. 2)-Duff in Love
    Codeforces Round #327 (Div. 2)-Wizards' Duel
  • 原文地址:https://www.cnblogs.com/xmxj0707/p/9671003.html
Copyright © 2011-2022 走看看