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

Example:

Input: [,null,,]

    \

    /

Output: [,,]

Follow up: Recursive solution is trivial, could you do it iteratively?

方法一:利用两个栈s1,s2来实现,先将头结点入栈s1,从s1弹出栈顶节点记为cur,压入s2中,分别将cur的左右孩子压入s1,当s1为空后,s2的弹出节点次序就是后序遍历的次序。(C++)

 vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> s1,s2;
s1.push(root);
vector<int> res={};
if(!root)
return res;
TreeNode* cur;
while(!s1.empty()){
cur=s1.top();
s1.pop();
s2.push(cur);
if(cur->left)
s1.push(cur->left);
if(cur->right)
s1.push(cur->right);
}
while(!s2.empty()){
cur=s2.top();
s2.pop();
res.push_back(cur->val);
}
return res;
}

方法二:先将头结点压入栈,怎样判断是该结点是应该输入vector中还是应该处理他的孩子?

1.该节点左右孩子为空时,为叶子结点,则该次遍历是输入到vector中

2.上一次输入的结点为该节点右孩子时,说明该结点的子树处理完毕,这次遍历是输入vector中

3.如果上一次输入的结点为该节点的左孩子,且右孩子为空,则该结点处理完毕,这次遍历就是输入vector中

4.否则说明子树没有被访问,按右、左孩子入栈。(C++)

 vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> s;
vector<int> res={};
if(!root)
return res;
s.push(root);
TreeNode* last=NULL;
TreeNode* top;
while(!s.empty()){
top=s.top();
if((top->left==NULL&&top->right==NULL)||(top->right==NULL&&last==top->left)||(last==top->right&&last!=NULL)){
res.push_back(top->val);
last=top;
s.pop();
}
else{
if(top->right)
s.push(top->right);
if(top->left)
s.push(top->left);
}
}
return res;
}

注意此时要加上这个判定条件,若不加,输出的为2,1,没有把3这个结点入栈

方法三:递归方法(C++)

 void postOrder(TreeNode* root,vector<int> &res){
if(!root)
return;
postOrder(root->left,res);
postOrder(root->right,res);
res.push_back(root->val);
} vector<int> postorderTraversal(TreeNode* root) {
vector<int> res={};
if(!root)
return res;
postOrder(root,res);
return res;
}

LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++的更多相关文章

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

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

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

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

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

    题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...

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

    这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...

  5. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

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

  6. 145 Binary Tree Postorder Traversal 二叉树的后序遍历

    给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解 ...

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

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

  8. lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历

    题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...

  9. Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历

    给定一个二叉树,返回它的 后序 遍历. 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; ve ...

随机推荐

  1. 搭建开发环境2)Debian8 安装jdk 1.8

    1.由于Debian自带了openjava运行时需要先卸载掉 java -version //查看当前java版本 apt-get remove openjdk* //卸载当前的openjdk 2.下 ...

  2. 在n个任意不相同的数中,输出r个数的组合,并且n和r由键盘输入。

    主要是运用递归的思想,函数主要两个参数,point是上一次取到的位置,picked主要记录在数组b中所取的元素的个数,函数的每一轮递归都会取一个数.   package pack; import ja ...

  3. 在树莓派2或3的kali上 RCA(a/v connector)接口的正确使用方法(多图)(原创)

                                    AV接口又称(RCA),AV接口算是出现比较早的一种接口,它由黄.白.红三种颜色的线组成,其中黄线为视频,红色为左声道,白色为右声道. ...

  4. vue添加class类名

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  5. 《DenseNet Models for Tiny ImageNet Classification》课程设计论文

    paper : <DenseNet Models for Tiny ImageNet Classification>     https://arxiv.org/ftp/arxiv/pap ...

  6. uclibc,eglibc,glibc,Musl-libc之间的区别和联系

    转自:https://www.sohu.com/a/164202127_424963 1.Glibc glibc = GNU C Library 是GNU项(GNU Project)目,所实现的 C语 ...

  7. 【SQL触发器】类型 FOR 、AFTER、 Instead of

    1.AFTER(for)触发器 (操作后) after触发器是指在操作成功后,所采取的一些动作! 比如:下面是我创建好的一个after触发器 creat trigger [dbo].[T_Carego ...

  8. C 标准库头文件

    头文件 说明 头文件 说明 <assert.h> 条件编译宏,将参数与零比较 <complex.h> (C99 起) 复数运算 <ctype.h> 用来确定包含于字 ...

  9. voc-fcn-alexnet网络结构理解

    一.写在前面 fcn是首次使用cnn来实现语义分割的,论文地址:fully convolutional networks for semantic segmentation 实现代码地址:https: ...

  10. 如何使用linux+xvfb+python+rfs+firefox+jenkins实现UI自动化

    首先说一下背景,在项目中使用windows执行机进行rfs脚本运行时,遇到两个问题: 1.Jenkins的多个slave节点和master的连接通过windows插件去建立,如果出现slave断连,无 ...