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?
递归方法
- /**
- * 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) {
- if(root==NULL)
- return v;
- TreeNode *left=root->left,*right=root->right;
- postorderTraversal(left);
- postorderTraversal(right);
- v.push_back(root->val);
- return v;
- }
- vector<int> v;
- };
非递归
处理并出栈时判断,该节点是否有子节点或其子节点是否被访问过。并记录上次处理的节点。
- /**
- * 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> v;
- if(root==NULL)
- return v;
- stack<TreeNode *> tree;
- tree.push(root);
- TreeNode *pre=NULL;
- while(!tree.empty()){
- TreeNode *p = tree.top();
- if((p->left==NULL&&p->right==NULL)||(pre!=NULL&&(p->left==pre||p->right==pre))){
- v.push_back(p->val);
- pre=p;
- tree.pop();
- }else{
- if(p->right!=NULL)
- tree.push(p->right);
- if(p->left!=NULL)
- tree.push(p->left);
- }
- }
- return v;
- }
- };
binary-tree-postorder-traversal——二叉树后续遍历的更多相关文章
- leetcode Binary Tree Postorder Traversal 二叉树后续遍历
先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
随机推荐
- luogu3834 【模板】可持久化线段树 1(主席树)
关于空间,第零棵树是 \(4n\),其后每棵树都要多来 \(\log(n)\) 的空间,所以我是开 \(n(4+\log(n))\) 的空间. 关于借用节点: 图片来自这里 #include < ...
- Django之model admin自定义后台管理
Admin管理界面是django的杀手级应用.它读取你模式中的元数据,然后提供给你一个强大而且可以使用的界面,网站管理者可以用它立即向网站中添加内容. 比如,数据表如下: from django.db ...
- Centos7 安装配置优化mysql(mariadb分支)
1.说明 由于在centos7的yum仓库中没有mysql,centos7用mariadb替代了mysql. mariadb是mysql源代码的一个分支, mysql被ORACLE闭源,而mariad ...
- 83. Spring Boot 1.4单元测试【从零开始学Spring Boot】
在[27. Spring Boot Junit单元测试]中讲过1.3版本的单元测试方式,这里说说1.4和1.3有什么区别之处? 在1.3中单元测试这样子的类似代码: //// SpringJUnit支 ...
- mybatis学习(二)——环境搭建
开发环境搭建主要包括以下几步 1.新建一个JAVA项目(可以只建一个文件夹) 2.导入jar包 log4j是一个日志包,可以不加,这里为了定位问题添加了该包,下面两个包必须需要. 3.创建数据库 C ...
- 将一个list均分成n个list
/** * 将一个list均分成n个list,主要通过偏移量来实现的 * @param source * @return */ public <T> List<List<T&g ...
- 自己写的java返回结果集封装
import java.io.Serializable; import com.fasterxml.jackson.core.JsonProcessingException; import com.f ...
- 花匠(codevs 3289)
题目描述 Description 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希望剩下的花 ...
- c/s委托练习
今天玩了玩C/S开发,也随便练习了很久不用的委托 父窗体中写的代码 #region 委托与事件传递 public delegate void TextChangedHandler(string ...
- 【Codeforces Round #503 (Div. 2)】
A:https://www.cnblogs.com/myx12345/p/9843198.html B:https://www.cnblogs.com/myx12345/p/9843245.html ...