LeetCode Binary Tree PostorderTranversal
Problem Description
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree{1,#,2,3}
,1\2/3return
[3,2,1]
.
Note: Recursive solution is trivial, could you do it iteratively?
Problem Solution
1. 递归方案
/*** Definition for binary tree* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/ class Solution { private:vector<int> nodeVec; public:void traverse(TreeNode *root){if(root==NULL)return;traverse(root->left);traverse(root->right);nodeVec.push_back(root->val);}vector<int> postorderTraversal(TreeNode *root) {traverse(root);return nodeVec;} };
2. 非递归方案
/*** Definition for binary tree* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/ class Solution { private:vector<int> nodeVec; public:void iterTraverse(TreeNode *root){if(root==NULL)return;stack<TreeNode*> st;TreeNode *pCur,*pPrev=NULL; //pCur: current tree node, pPrev: previous visited tree node st.push(root);while(!st.empty()){pCur=st.top();if((pCur->left == NULL && pCur->right == NULL) || (pPrev != NULL && (pCur->left==pPrev || pCur->right==pPrev))){nodeVec.push_back(pCur->val); pPrev=pCur;st.pop();}else{if(pCur->right != NULL)st.push(pCur->right);if(pCur->left != NULL)st.push(pCur->left);}}}vector<int> postorderTraversal(TreeNode *root) {iterTraverse(root);return nodeVec;} };
转载于:https://www.cnblogs.com/ballwql/p/3668357.html
LeetCode Binary Tree PostorderTranversal相关推荐
- [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)
目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...
- Leetcode | Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- LeetCode Binary Tree Right Side View(搜索)
问题:给出一个二叉树,要求输出右视图 思路:因为要求输出右视图.可以考虑使用深度优先搜索或者 广度优先搜索. 使用深度优先搜索时,以非递归形式,将左右子树入栈,同时使用哈希表记录深度与对应右视图的值. ...
- LeetCode Binary Tree Preorder Traversal(二叉树的前序遍历)
问题:给出一个二叉树,输出前序遍历 思路: 自顶向下遍历过程中,将当前结点的值加入到list中,然后处理左.右子树 具体代码参考: https://github.com/wuli2496/OJ/tre ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [LeetCode] Binary Tree Postorder题解
Binary Tree Postorder Given a binary tree, return the postorder traversal of its nodes' values. For ...
- [Leetcode] Binary Tree Maximum Path Sum
这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...
- [leetcode] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- LeetCode Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
最新文章
- LeetCode简单题之统一一致字符串的数目
- 三维目标检测算法原理
- Mybatis 获取当前序列和下一个序列值 以及在一个方法中写多条SQL 语句
- C++又一坑:动态链接库中的全局变量
- ejb 2.0 3.0_Java EE 6测试第I部分– EJB 3.1可嵌入API
- java某个时间推迟60天_java计算两个时间相差(天、小时、分钟、秒)
- java weblogic多数据源,039 在weblogic下部署jndi的多数据源
- Java基础学习总结(30)——Java 内存溢出问题总结
- 计算机机房系统安装,学校机房轻松安装WIN7系统.doc
- 数学分析(一)数列与数列极限
- outlook邮箱撤回邮件
- 分享几个影视采集网站
- UDP/ICMP协议简介
- 第一次使用acme.sh 手动生成证书(DNS手动模式)
- html5关于校庆作品名称,五十五周年校庆标语
- 纽约州立大学水牛城分校计算机科学专业,纽约州立大学水牛城分校有哪些强势专业...
- 如何将iPhone升级到ios 14
- python第二周基本图形绘制
- css30主要作用,30个你不可不知的CSS选择器小结
- 封神台—高校靶场ctf—第一期Web web_008