[LeetCode]145.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?

【代码】

/*********************************
*   日期:2014-12-07
*   作者:SJF0115
*   题号: 145.Binary Tree Postorder Traversal
*   来源:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <malloc.h>
#include <vector>
#include <stack>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> v;
        stack<TreeNode *> stack;
        TreeNode *p = root;
        TreeNode *q;
        do{
            //遍历左子树
            while(p != NULL){
                stack.push(p);
                p = p->left;
            }
            q = NULL;
            while(!stack.empty()){
                p = stack.top();
                stack.pop();
                // 右子树是否为空或者已访问过
                if(p->right == q){
                    v.push_back(p->val);
                    //保留访问过的节点
                    q = p;
                }
                else{
                    //当前节点不能访问,p节点重新入栈
                    stack.push(p);
                    //处理右子树
                    p = p->right;
                    break;
                }//if
            }//while
        }while(!stack.empty());//while
        return v;
    }
};

//按先序序列创建二叉树
int CreateBTree(TreeNode* &T){
    char data;
    //按先序次序输入二叉树中结点的值(一个字符),‘#’表示空树
    cin>>data;
    if(data == '#'){
        T = NULL;
    }
    else{
        T = (TreeNode*)malloc(sizeof(TreeNode));
        //生成根结点
        T->val = data-'0';
        //构造左子树
        CreateBTree(T->left);
        //构造右子树
        CreateBTree(T->right);
    }
    return 0;
}

int main() {
    Solution solution;
    TreeNode* root(0);
    CreateBTree(root);
    vector<int> v = solution.postorderTraversal(root);
    for(int i = 0;i < v.size();i++){
        cout<<v[i]<<endl;
    }
}

【代码二】

/*------------------------------------------------
*   日期:2015-03-25
*   作者:SJF0115
*   题目: 145.Binary Tree Postorder Traversal
*   来源:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/
*   结果:AC
*   来源:LeetCode
*   博客:
------------------------------------------------------*/
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

// 二叉树节点结构
struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x):val(x),left(nullptr),right(nullptr){}
};

class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> result;
        if(root == nullptr){
            return result;
        }//if
        stack<TreeNode*> s;
        s.push(root);
        TreeNode *node;
        while(!s.empty()){
            node = s.top();
            s.pop();
            result.insert(result.begin(),node->val);
            // 左子树
            if(node->left){
                s.push(node->left);
            }//if
            // 右子树
            if(node->right){
                s.push(node->right);
            }//if
        }//while
        return result;
    }
};
// 1.创建二叉树
void CreateTree(TreeNode* &root){
    int val;
    //按先序次序输入二叉树中结点的值,‘-1’表示空树
    cin>>val;
    // 空节点
    if(val == -1){
        root = nullptr;
        return;
    }//if
    root = new TreeNode(val);
    //构造左子树
    CreateTree(root->left);
    //构造右子树
    CreateTree(root->right);
}
int main() {
    freopen("C:\\Users\\Administrator\\Desktop\\c++.txt", "r", stdin);
    TreeNode* root = nullptr;
    vector<int> result;
    // 创建二叉树
    CreateTree(root);

    Solution solution;
    result = solution.postorderTraversal(root);
    for(int i = 0;i < result.size();++i){
        cout<<result[i]<<" ";
    }
    cout<<endl;
    return 0;
}

时间: 2024-07-30 18:37:12

[LeetCode]145.Binary Tree Postorder Traversal的相关文章

[LeetCode 第12题] -- Binary Tree Postorder Traversal

题目链接: Binary Tree PostOrder Trveral 题目意思: 给定一棵二叉树,求后续遍历序列 代码: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public:

[LeetCode]94.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? &

[LeetCode]144.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? [代码一] /*******************************

[LeetCode] Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 递归实现代码 /*********************************************

[LeetCode 第8题] -- Binary Tree Preorder Traversal

题目链接: Binary Tree Preorder Traversal 题目意思: 给定一个二叉树根节点,求前序序列 代码: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { publi

[LeetCode] Maximum Binary Tree 最大二叉树

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number in the array. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number

[LeetCode] Print Binary Tree 打印二叉树

Print a binary tree in an m*n 2D string array following these rules: The row number m should be equal to the height of the given binary tree. The column number n should always be an odd number. The root node's value (in string format) should be put i

[LeetCode]103.Binary Tree Zigzag Level Order Traversal

[题目] Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 retur

[LeetCode]102.Binary Tree Level Order Traversal

[题目] Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], [15,7