详见:剑指 Offer 题目汇总索引:第6题

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?

注:后序遍历是较麻烦的一个,不可大意。关键两点: 1.要走到 p->left | p->right ==0, 2.每次出栈出两个结点。

/**
* Definition for binary tree
* 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> answer;
if(root == NULL) return answer;
stack<TreeNode*> st;
st.push(root);
TreeNode *p = root;
while(p->right || p->left) {
while(p->left) { st.push(p->left); p = p->left;}
if(p->right) { st.push(p->right); p = p->right;}
}
while(!st.empty()) {
TreeNode *q = st.top(); st.pop();
answer.push_back(q->val);
if(!st.empty()) {
TreeNode *q2 = st.top();
while(q2->right && q2->right != q) {
st.push(q2->right); q2 = q2->right;
while(q2->left || q2->right) {
while(q2->left){ st.push(q2->left); q2 = q2->left;}
if(q2->right){ st.push(q2->right); q2 = q2->right;}
}
}
}
}
return answer;
}
};

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?

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> vec;
if(root == NULL) return vec;
stack<TreeNode*> st;
st.push(root);
while(!st.empty()) {
TreeNode *p = st.top(); st.pop();
vec.push_back(p->val);
if(p->right) st.push(p->right);
if(p->left) st.push(p->left);
}
return vec;
}
};

12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal的更多相关文章

  1. [Swift]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal

    Return the root node of a binary search tree that matches the given preorder traversal. (Recall that ...

  2. LeetCode 1008. Construct Binary Search Tree from Preorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 题目: Retu ...

  3. 【leetcode】1008. Construct Binary Search Tree from Preorder Traversal

    题目如下: Return the root node of a binary search tree that matches the given preorder traversal. (Recal ...

  4. 【LeetCode】1008. Construct Binary Search Tree from Preorder Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

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

  6. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  7. Binary Tree Postorder Traversal leetcode java

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

  8. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

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

  9. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

随机推荐

  1. 跨域资源共享 CORS

    CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...

  2. register based 和 stack based虚拟机的区别

    其实其核心的差异,就是Dalvik 虚拟机架构是 register-based,与 Sun JDK 的 stack-based 不同,也就是架构上的差异.我先摘录几段网上可以找到的资料,重新整理和排版 ...

  3. Rhel6-vpn配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.160 server60.example.com 192.168.12 ...

  4. HTML中head里的内容经浏览器解析后全到body里

    我从linux服务器nginx上把一个网站迁移到windows的IIS上数据什么的都么有问题,配置好rewrite以后,访问网站,发现样式变动了,网站上方空出了一块我用chrome浏览器的审查元素一看 ...

  5. NAT模式下用secureCRT连接虚拟机

    VMWare制作学习系统,或布置模拟网络群组环境,已经比较流行. 注意主机端口要设置成为不同2122,虚拟机端口设置成22 笔者为了给项目组同事,提供一个练习ssh远程连接操作,及方便抓图交流的环境, ...

  6. 在 Xcode 6 中使用矢量图( iPhone 6 置配 UI)

    在 Xcode 6 中使用矢量图( iPhone 6 置配 UI) (本文转载:http://iosdeveloper.diandian.com/post/2014-09-25/40063062789 ...

  7. Linux多人群聊系统(简单多线程服务器)

    一:要求 1.通过一个服务器实现最多5个客户之间的信息群发. 2.服务器显示客户的登录与退出: 3.客户连接后首先发送客户名称,之后发送群聊信息: 4.客户输入bye代表退出,在线客户能显示其他客户的 ...

  8. web移动端input获得光标Fixed定位失效解决方案

    移动端业务开发,iOS 下经常会有 fixed 元素和输入框(input 元素)同时存在的情况. 但是 fixed元素在有软键盘唤起的情况下,会出现许多莫名其妙的问题. 这篇文章里就提供一个简单的有输 ...

  9. uva147 Dollars ——完全背包

    link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  10. 解析HTTP协议六种请求方法,get,head,put,delete,post有什么区别

    GET: 请求指定的页面信息,并返回实体主体.HEAD: 只请求页面的首部.POST: 请求服务器接受所指定的文档作为对所标识的URI的新的从属实体.PUT: 从客户端向服务器传送的数据取代指定的文档 ...