【遍历二叉树】03二叉树的后序遍历【Binary Tree Postorder Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
给定一个二叉树,返回他的后序遍历的节点的values。
例如:
给定一个二叉树 {1,#,2,3}
,
- 1
- \
- 2
- /
- 3
返回 [3,2,1]
.
笔记:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#include <iostream>
#include <cstdio> #include <stack> #include <vector> #include "BinaryTree.h" using namespace std; /** vector<int> path; // 树中结点含有分叉, ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3); PrintTree(pNodeA1); vector<int> ans = postorderTraversal(pNodeA1); for (int i = 0; i < ans.size(); ++i) DestroyTree(pNodeA1); |
2.非递归实现
后序遍历的非递归实现是三种遍历方式中最难的一种。因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来了难题。下面介绍两种思路。
第一种思路:对于任一结点P,将其入栈,然后沿其左子树一直往下搜索,直到搜索到没有左孩子的结点,此时该结点出现在栈顶,但是此时不能将其出栈并访问, 因此其右孩子还为被访问。所以接下来按照相同的规则对其右子树进行相同的处理,当访问完其右孩子时,该结点又出现在栈顶,此时可以将其出栈并访问。这样就 保证了正确的访问顺序。可以看出,在这个过程中,每个结点都两次出现在栈顶,只有在第二次出现在栈顶时,才能访问它。因此需要多设置一个变量标识该结点是 否是第一次出现在栈顶。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
#include <iostream>
#include <cstdio> #include <stack> #include <vector> #include "BinaryTree.h" using namespace std; /** //中介节点 //非递归后序遍历-迭代 // 树中结点含有分叉, ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3); PrintTree(pNodeA1); vector<int> ans = postorderTraversal(pNodeA1); for (int i = 0; i < ans.size(); ++i) DestroyTree(pNodeA1); |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_ struct TreeNode TreeNode *CreateBinaryTreeNode(int value); #endif /*_BINARY_TREE_H_*/ |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
#include <iostream>
#include <cstdio> #include "BinaryTree.h" using namespace std; /** //创建结点 return pNode; //连接结点 //打印节点内容以及左右子结点内容 if(pNode->left != NULL) if(pNode->right != NULL) printf("\n"); //前序遍历递归方法打印结点内容 if(pRoot != NULL) if(pRoot->right != NULL) void DestroyTree(TreeNode *pRoot) delete pRoot; DestroyTree(pLeft); |
【遍历二叉树】03二叉树的后序遍历【Binary Tree Postorder Traversal】的更多相关文章
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- 1058FBI<二叉树,递归,后序遍历>
问题描述 我们可以把由"0"和"1"组成的字符串分为三类:全"0"串称为B串,全"1"串称为I串,既含"0&q ...
- [LeetCode] 145. 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)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- URAL 1136 Parliament 二叉树水题 BST后序遍历建树
二叉树水题,特别是昨天刚做完二叉树用中序后序建树,现在来做这个很快的. 跟昨天那题差不多,BST后序遍历的特型,找到最后那个数就是根,向前找,比它小的那块就是他的左儿子,比它大的那块就是右儿子,然后递 ...
- [Swift]LeetCode145. 二叉树的后序遍历 | Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
随机推荐
- Unity 游戏对象消失 enable,destroy与active的区别
gameObject.SetActive(false):是否在场景中停用该物体,停用后Hierarchy窗口呈灰色,用Find函数也找不到.如果该物体有子物体,要用SetActive Recursir ...
- display getSize()
If you want the the display dimensions in pixels you can use getSize: Display display = getWindowMan ...
- iOS开发之获取系统相册ALAssetLibrary
注:当你选择看这篇博客时想必你的应用还支持iOS8一下系统,如果你的应用要求最低版本大于iOS8,建议使用PhotoKit框架,效率更高 ALAssetsLibrary包含,ALAssetsLibra ...
- 【python】-- 初识python
Python 安装 windows: 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装路径:C:\python27 3.配置环境变量 [右键计算机 ...
- mysql mariadb 乱码
mysql 创建临时表 CREATE TEMPORARY TABLE tmp_table SELECT COUNT(*) AS num FROM student_info GROUP BY LEFT( ...
- 【TFS】解决TFS编译中文乱码问题
前言; TFS2018做程序集成非常方便,线上编译然后直接生成docker镜像,但是在使用过程中遇到编译窗口中文乱码的问题,这个问题找了好久没人知道怎么解决.如下: 这个问题不解决,每次编译失败,研发 ...
- iview 表单相关
view表单验证的步骤: 第一步:给 Form 设置属性 rules :rules第二步:同时给需要验证的每个 FormItem 设置属性 prop 指向对应字段即可 prop=”“第三步:注意:Fo ...
- Docker容器部署tomcat出现中文乱码
docker 容器部署tomcat后,日志文件中出现中文乱码,很多问号,中文的文件夹也是问好.先看看容器的locale: [root@docker1 ~]# docker exec -it 41de9 ...
- [原创]java WEB学习笔记42:带标签体的自定义标签,带父标签的自定义标签,el中自定义函数,自定义标签的小结
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- IAR 条件断点
条件断点是IDE的一个重要功能,在IAR调试时候,经常跟踪一个数据,但是对较大的buffer,用普通的断点或live watch都不好跟踪. 比如某个buffer里一个数,我们知道他在第几个,但是却从 ...