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?

class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> result;
if(!root) return result; stack<MyNode*> treeStack;
MyNode* myRoot = new MyNode(root);
MyNode* current, *newNode;
treeStack.push(myRoot); while(!treeStack.empty())
{
current = treeStack.top();
treeStack.pop();
if(!current->flag)
{
current->flag = true;
treeStack.push(current);
if(current->node->right) {
newNode = new MyNode(current->node->right);
treeStack.push(newNode);
}
if(current->node->left) {
newNode = new MyNode(current->node->left);
treeStack.push(newNode);
}
}
else
{
result.push_back(current->node->val);
}
}
return result; }
struct MyNode
{
TreeNode* node;
bool flag; //indicate if the node has been visited
MyNode(TreeNode* x) : node(x),flag(false) {}
};
};

法II: 不重新定义结构,以root->right->left的顺序访问节点,最后逆序。

/**
* 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:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
if(!root) return result; stack<TreeNode*> treeStack;
TreeNode* current;
treeStack.push(root); //visit in order root->right->left
while(!treeStack.empty())
{
current = treeStack.top();
treeStack.pop();
result.push_back(current->val);
if(current->left) treeStack.push(current->left);
if(current->right) treeStack.push(current->right);
} //reverse result
int size = result.size();
int hsize = size>>;
int tmp;
for(int i = ; i < hsize; i++){
tmp = result[i];
result[i]=result[size--i];
result[size--i]=tmp;
}
return result;
}
};

145. Binary Tree Postorder Traversal (Stack, Tree)的更多相关文章

  1. [LeetCode] N-ary Tree Postorder Traversal N叉树的后序遍历

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

  2. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  3. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  4. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  5. 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  6. 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal

    详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  7. LeetCode: Binary Tree Postorder Traversal 解题报告

    Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...

  8. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  9. LeetCode:145_Binary Tree Postorder Traversal | 二叉树后序遍历 | Hard

    题目:Binary Tree Postorder Traversal 二叉树的后序遍历,题目要求是采用非递归的方式,这个在上数据结构的课时已经很清楚了,二叉树的非递归遍历不管采用何种方式,都需要用到栈 ...

随机推荐

  1. C#.NET抽象类和接口的区别?

    声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现该类的情况.不能创建abstract 类的实例.然 ...

  2. 【转】Linux动态链接(4)ldd与ldconfig

    原文网址:http://tsecer.blog.163.com/blog/static/15018172012414105551345/ 一.动态链接工具ldd和ldconfig是动态链接的两个重要辅 ...

  3. Spark的CombineByKey

    combineBykey关键是要明白里面的三个函数: 1. 当某个key第一次出现的时候,走的是第一个函数(createCombin):A function that creates a combin ...

  4. HTTP 报文首部

    1.HTTP 报文首部 HTTP 协议的请求和响应报文中必定包含 HTTP 首部.首部内容为客户端和服务器分别处理请求和响应提供所需要的信息. 1)HTTP请求报文:在请求中,HTTP报文由方法.UR ...

  5. 第十一章 Helm-kubernetes的包管理器(上)

    Helm - K8s的包管理器 11.1 Why Helm K8s能够很好的组织和编排容器,但它缺少一个更高层次的应用打包工具,Helm就是干这个的. 比如对于一个MySQL服务,K8s需要部署如下对 ...

  6. vscode新版1.31.1使用代码检查工具ESlint支持VUE

    1.VSCODE中安装ESlint省略 2.菜单文件->首选项->设置->扩展->ESLint 打钩:Eslint:Auto Fix On Save 点击此链接:在settin ...

  7. Linux Platform devices 平台设备驱动

    设备总线驱动模型:http://blog.csdn.net/lizuobin2/article/details/51570196 本文主要参考:http://www.wowotech.net/devi ...

  8. 字体相关CSS属性介绍

    font-family 字体系列. font-family可以把多个字体名称作为一个“回退”系统来保存.如果浏览器不支持第一个字体,则会尝试下一个.浏览器会使用它可识别的第一个值. 简单实例: bod ...

  9. socket通信循环

    server-----------------#!/usr/bin/env python # encoding: utf-8  # Date: 2018/6/5 import socket phone ...

  10. sqlldr并发

    sage: SQLLDR keyword=value [,keyword=value,...]   部分关键字:     userid -- ORACLE username/password    c ...