Binary Tree Postorder Traversal

原创 2016年08月29日 11:43:26

一、问题描述

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].

二、思路

二叉树的后续遍历,非递归遍历稍微复杂一点,需要另外设置一个数据结构,改天补上,未完待续。

三、代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void postorder(TreeNode* node,vector<int> &res){
        if(!node){
            return ;
        }else{
            postorder(node -> left,res);
            postorder(node -> right,res);
            res.push_back(node -> val);
        }
    }
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> vec;
        postorder(root,vec);
        return vec;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

Binary Tree Postorder Traversal - LeetCode 145

题目描述: Given a binary tree, return the postorder traversal of its nodes' values. For example: Gi...
  • bu_min
  • bu_min
  • 2015年05月22日 23:01
  • 200

Construct Binary Tree from Inorder and Postorder Traversal

题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assu...
  • Boyhou
  • Boyhou
  • 2013年07月28日 15:53
  • 828

Construct Binary Tree from Inorder and Postorder Traversal(medium)

【题目】             Given inorder and postorder traversal of a tree, construct the binary tree. ...
  • haihaa
  • haihaa
  • 2016年05月23日 10:35
  • 112

LeetCode Week9: Binary Tree Preorder/InOrder/PostOrder Traversal

这一周完成的题目主要是Tree部分的题目,这里选择3道经典的题来编写题解,分别是Binary Tree Preorder Traversal、Binary Tree Inorder Traversal...

Binary Tree Postorder Traversal

题目 Given a binary tree, return the postorder traversal of its nodes' values. For example...

【LEETCODE】106-Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume t...

LeetCode OJ: Binary Tree Postorder Traversal

转战LeetCode,开始不适应,只好先从简单的题目开始 Given a binary tree, return the postorder traversal of its nodes' value...

[Leetcode] Construct Binary Tree from Inorder and Postorder/Preorder and Inorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume t...

Binary Tree Postorder Traversal leetcode145

Given a binary tree, return the postorder traversal of its nodes’ values.For example: Given binary...

LeetCode 7 Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Binary Tree Postorder Traversal
举报原因:
原因补充:

(最多只允许输入30个字)