详见:剑指 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. CAN基础知识

    CAN:Controller Area Network,是ISO国际标准化的串行通信协议. CAN控制器根据两根线上的电位来判断总线电平.总线电平分为显性电平和隐性电平,二者必居其一.发送方通过使总线 ...

  2. 收藏的牛人的Backbone分享教程

    http://yujianshenbing.iteye.com/category/256978 感谢御剑神兵,目前正在看,为源码分析做准备. 今天是2015年4月13号,看了前两篇,

  3. iframe自定义高度

    function setIframeHeight() { var iframe=document.getElementById("iframe_id"); iframe.heigh ...

  4. Android 绿豆蛙版连连看(简陋版)

    (里面有六张绿豆蛙的图片) 1.选中会有红色框 2.可以直线连(横竖相邻或是横竖间隔空格) 3.可以拐一次弯连接 4.可以拐两次弯连接 5.连接时会有线显示 6.绿色代表进度条,蓝色代表时间条 imp ...

  5. Ubuntu下快速安装LAMP server

    Ubuntu下可快速安装LAMP server(Apache+MySQL+PHP5). 首先,打开Ubuntu虚拟机,Terminal打开root权限:“sudo -s”. 一.安装LAMP serv ...

  6. jQueryUI 之控件们

    总结:总的来说,这些控件可以在官网找到列子, 部分ui效果不如意的,可根据jQueryUI上添加的类选择器等,进行再加工 <!DOCTYPE html> <html> < ...

  7. Startup key combinations for Intel-based Macs

    Learn about the startup key combinations you can use with Intel-based Macs. You can use the followin ...

  8. Python 基礎 - 字符編碼

    Python 解釋器在加載 .py 文件中的代碼時,會對內容進行編碼 (默認 ascill) ASCII (American Standard Code for Information Interch ...

  9. 关于配置服务器(IIS7)

    服务器Server2003 ,无限开机动画慢动作重播,一怒而重装server2008 然,重点就是系统装好了 IIS装好了 ,发布网站 开始各种错了!!! 1.第一个错 额,错误信息说 :由于权限不足 ...

  10. 数论 UVA 11076

    这道题目的意思简单易懂说的是给你n个数(可能有重复相同的数字),列出他们所有排列的情况,再逐位相加,求出和,例如:给你1,2,3,则排列的情况为<123>, <132>, &l ...