5.1.3—二叉树的遍历—Binary Tree Postorder Traversal
来源:互联网 发布:浪潮软件重大新闻 编辑:程序博客网 时间:2024/05/17 06:38
描述
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?
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?
#include "BinaryTree.h"#include <stack>#include<vector>using namespace std;//===二叉树的后序遍历,递归版本void PostorderTraversal(BinaryTreeNode *proot){if (proot){if (proot->m_pLeft)PostorderTraversal(proot->m_pLeft);if (proot->m_pRight)PostorderTraversal(proot->m_pRight);cout << proot->m_nValue << " ";}}//===二叉树的后序遍历,迭代版本void PostorderTraversal2(BinaryTreeNode *proot){stack<BinaryTreeNode*> temp;vector<int> cahe;BinaryTreeNode *p = proot,*q;bool flag;do{while (p){temp.push(p);p = p->m_pLeft;}flag = true;q = NULL;while ((!temp.empty()) && flag){p = temp.top();if (p->m_pRight == q){cahe.push_back(p->m_nValue);temp.pop();q = p;}else{p = p->m_pRight;flag = false;}}} while (!temp.empty());//======for (int i = 0; i < cahe.size(); i++)cout << cahe[i] << " ";cout << endl;}// ====================测试代码====================// 8// 6 10// 5 7 9 11int main(){BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);ConnectTreeNodes(pNode8, pNode6, pNode10);ConnectTreeNodes(pNode6, pNode5, pNode7);ConnectTreeNodes(pNode10, pNode9, pNode11);//===//PrintTree(pNode8);//===PostorderTraversal(pNode8);cout << endl;//===PostorderTraversal2(pNode8);//==DestroyTree(pNode8);}
阅读全文
0 0
- 5.1.3—二叉树的遍历—Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal-二叉树的后序遍历
- Binary Tree Postorder Traversal 二叉树的后序遍历
- 【二叉树的后续遍历】Binary Tree Postorder Traversal
- 二叉树的后序遍历 Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal 二叉树的后序遍历
- LeetCode145——Binary Tree Postorder Traversal二叉树后序遍历
- 二叉树后序遍历——145. Binary Tree Postorder Traversal
- Binary Tree Postorder Traversal 二叉树后续遍历@LeetCode
- binary-tree-postorder-traversal(后序遍历二叉树)
- 二叉树遍历145. Binary Tree Postorder Traversal
- leetcode 145. Binary Tree Postorder Traversal 二叉树后续遍历
- LeetCode Binary Tree Postorder Traversal(二叉树的后序遍历 非递归实现)
- Binary Tree Postorder Traversal 二叉树的后序遍历,使用堆栈,非递归
- LeetCode OJ 之 Binary Tree Postorder Traversal (二叉树的后序遍历)
- LintCode Binary Tree Postorder Traversal 二叉树的后序遍历
- LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
- Binary Tree Postorder Traversal 二叉树的后序遍历(迭代非递归版本)
- 上楼梯
- TF随笔-13
- HDU 6069 Counting Divisors【素数筛法】
- First Time
- 算法编程题-字符串类型题目
- 5.1.3—二叉树的遍历—Binary Tree Postorder Traversal
- 前 言 (全集PDF版下载)
- 5.1.4—二叉树的遍历—Binary Tree Level Order Traversal
- Union与Union All 区别
- 5.1.5—二叉树的遍历—Binary Tree Level Order Traversal II
- Android7.0中尝试传递 file:// URI 会触发 FileUriExposedException
- docker~swarm搭建docker高可用集群
- 嵌入式linux基础面试题
- 全球100款大数据工具汇总