[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:
    void getPostOrder (TreeNode *root, vector<int>& output);
    vector<int> postorderTraversal(TreeNode *root);
};

void Solution::getPostOrder(TreeNode *root, vector<int>& output) {
    if (NULL == root) {
        return;
    }
    getPostOrder(root->left, output);
    getPostOrder(root->right, output);
    output.push_back(root->val);
}

vector<int> Solution::postorderTraversal(TreeNode *root) {
    vector<int> output;
    output.clear();
    getPostOrder(root, output);
    return output;
}
时间: 2024-10-22 03:56:56

[LeetCode 第12题] -- Binary Tree Postorder Traversal的相关文章

[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]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? [代码] /*******************************

[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]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] Serialize and Deserialize Binary Tree

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput

[LeetCode] Maximum Width of Binary Tree 二叉树的最大宽度

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level

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].   Java代码    /**   * Definition for a binary tree node.   * public class TreeNode {   *     int val;   *   

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]. 二叉树前序遍历的非递归版本,C++实现代码: #include<iostream> #include<new> #include<vector> #include<stack>