原题地址递归写法谁都会,看看非递归写法。对于二叉树的前序和中序遍历的非递归写法都很简单,只需要一个最普通的栈即可实现,唯独后续遍历有点麻烦,如果不借助额外变量没法记住究竟遍历了几个儿子。所以,最直接的想法就是在栈中记录到底遍历了几个儿子。代码: 1 vector postorderTraversal...
分类:
其他好文 时间:
2015-01-19 18:40:29
阅读次数:
116
二叉树的非递归遍历:
中序遍历非递归遍历算法
非递归算法实现的基本思路:使用堆栈:...
分类:
其他好文 时间:
2015-01-18 13:13:54
阅读次数:
189
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1564分析:首先因为每个点的数据值不变,所以无论树的形态如何变,树的中序遍历肯定不变,就是所有数据值从小到大排。然后设f[i][j][v]表示中序遍历的i~j位组成一颗子树,其中要求权值都...
分类:
其他好文 时间:
2015-01-17 23:33:47
阅读次数:
349
数据结构期末复习第六章树和二叉树知识点:先序遍历二叉树规则:根-左-右1.访问根结点2.先序遍历左子树3.先序遍历右子树中序遍历二叉树规则:左-根-右1.先中序遍历左子树2.再访问根节点3.最后访问中序遍历右子树后序遍历二叉树规则:左-右-根1.后序遍历左子树2.后序遍历右子树3.访问根结点1. 一...
分类:
其他好文 时间:
2015-01-17 19:29:07
阅读次数:
239
题目:
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Calling next() will return the next smallest number in the BST.
Note:...
分类:
编程语言 时间:
2015-01-14 18:01:28
阅读次数:
182
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ 2 2
/ \ / 3 4 4 3
But the f...
分类:
其他好文 时间:
2015-01-14 11:08:56
阅读次数:
154
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
/**
* Defin...
分类:
其他好文 时间:
2015-01-14 09:48:40
阅读次数:
139
假设有棵树,长下面这个样子,它的前序遍历,中序遍历,后续遍历都很容易知道。
PreOrder: GDAFEMHZ
InOrder: ADEFGHMZ
PostOrder: AEFDHZMG
现在,假设仅仅知道前序和中序遍历,如何求后序遍历呢?比如,已知一棵树的前序遍历是”GDAFEMHZ”,而中序遍历是”ADEFGHMZ”应该...
分类:
其他好文 时间:
2015-01-13 23:19:04
阅读次数:
255
原题如下;
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
题意很简单:根据中序遍历和后序遍历的序列生成树
思路很简单:根据后序遍历的序列确定根节点,在中序遍历中找到根节点,将原来的序列分为左右两个部分,递归解决左右两个部分就可以解决这个问题。
Java代码如下:
publ...
分类:
其他好文 时间:
2015-01-07 01:52:25
阅读次数:
160
题目描述:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
解题思路:先序遍历的第一个元素为根元素,在中序遍历序列中找到根元素的位置。中...
分类:
其他好文 时间:
2015-01-06 11:58:44
阅读次数:
159