目录
1 求二叉树深度
1.1 递归实现
1.2 非递归实现(队列)
1.3 非递归实现(栈)
2 求二叉树高度
3 二叉树先序遍历
3.1 递归版本
3.2 非递归版本
3.3 利用先序创建二叉树
4 二叉树中序遍历
4.1 递归版本
4.2 非递归版本
5 二叉树后序遍历
5.1 递归版本
5.2 非递归版本
6 二叉树层序遍历
7 二叉树深度优先遍历
8 二叉树广度优先遍历
9 求二叉树节点间最大距离
10 计算二叉树叶子节点数目
11 获取二叉树中根节点到指定节点的路径
12 获取两条路径的最后一个共同节点
13 获取两个结点的最近共同祖先
本文总结关于二叉树的各种遍历算法
首先定义二叉树节点结构体
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
int TreeDepth(TreeNode* pRoot)
{
if(pRoot==NULL)
return 0;
int nleft = TreeDepth(pRoot->left);
int nright = TreeDepth(pRoot->right);
return (nleft>nright)?(nleft+1):(nright+1);
}
len
记录该层的结点个数,也就是队列的当前长度,然后依次在队列中访问该层的len
个结点(将队列中len
个元素出队列),并将下一层入队列。int TreeDepth(TreeNode* pRoot)
{
queue q;
if(!pRoot) return 0;
q.push(pRoot);
int level=0;
while(!q.empty()){
int len=q.size();
level++;
while(len--){
TreeNode* tem=q.front();
q.pop();
if(tem->left) q.push(tem->left);
if(tem->right) q.push(tem->right);
}
}
return level;
}
#include
#include
using namespace std;
typedef struct BinTree
{
int data;
BinTree *lc;
BinTree *rc;
}BTNode,*BinTree;
int max(int a,int b)
{
return (a>b)?a:b;
}
int BinTreeDepth(BinTree T)
{
stack s;
BinTree p = T,r = NULL;
int depth=0;
while(p||!s.empty())
{
if(p)
{ //从根节点向左边走
s.push(p);
int size = s.size();//获取栈的大小
depth = max(depth,size);//替换最大值
p = p->lc;
}
else
{ //左边走不通向右走
p = s.top();
if(p->rc&&p->rc!=r)//如果右子树存在,且未被访问过
{
p = p->rc;
s.push(p);
int size = s.size();//获取栈的大小
depth = max(depth,size);//替换最大值
p = p->lc;
}else
{
p=s.top();
s.pop();
cout<data<
跟求深度一样,唯一的区别就是,对于根节点,深度为1,高度为0.
int Height(TreeNode *node)
{
if(node == NULL)
return -1;
else
return 1 + Max(Height(node->left),Height(node->right));
}
void preOrderTraverse(TreeNode *root) {
if (root != NULL) {
cout<val<left);
preOrderTraverse(root->right);
}
}
void PreOrder(TreeNode* pRoot)//前序遍历,非递归
{
if (pRoot == NULL)
return;
stack s;
TreeNode *p = pRoot;
while (p != NULL || !s.empty())
{
if (p != NULL)
{
cout << p->val << " ";
s.push_back(p);
p = p->left;
}
else
{
p = s.pop();
p = p->right;
}
}
cout << endl;
}
//前序创建一颗二叉树
void CreateBiTree(BiTree &T)
{
char c;
cin >> c;
if ('#' == c)
T=NULL;
else
{
T = (BiNode* ) malloc(sizeof(BiNode));
T->val = c;
CreateBiTree(T->left);
CreateBiTree(T->right);
}
}
void inOrderTraverse(TreeNode* root) {
if (root != NULL) {
inOrderTraverse(root->left);
cout<val<<" ";
inOrderTraverse(root->right);
}
}
void inOrderTraverse(TreeNode* root) {
stack s;
TreeNode* pNode = root;
while (pNode != null || !stack.isEmpty()) {
if (pNode != null) {
s.push(pNode);
pNode = pNode->left;
} else { //pNode == null && !stack.isEmpty()
TreeNode node = s.pop();
cout<val<<" ";
pNode = node->right;
}
}
}
public void postOrderTraverse(TreeNode root) {
if (root != null) {
postOrderTraverse(root.left);
postOrderTraverse(root.right);
System.out.print(root.val+" ");
}
}
void PostOrder(BiTree &T)//后序遍历(双栈法),非递归
{
if (T == NULL)
return;
vector S1,S2;
BiNode *p ;//当前指针所在结点
S1.push_back(T);
while (!S1.empty())
{
p = S1[S1.size() - 1];
S1.pop_back();
S2.push_back(p);
if (p->left)
S1.push_back(p->left);
if (p->right)
S1.push_back(p->right);
}
while (!S2.empty())
{
cout << S2[S2.size() - 1]->val << " ";
S2.pop_back();
}
cout << endl;
}
void levelTraverse(TreeNode* root) {
if (root == NULL) {
return;
}
queue q;
queue.push_back(root);
while (!q.empty()) {
TreeNode* node = q.pop_front();
cout<val<<" ";
if (node->left != NULL) {
q.push_back(node->left);
}
if (node->right != NULL) {
q.push_back(node->right);
}
}
}
深度优先遍历就是先序遍历。
//深度优先遍历
void depthFirstSearch(Tree root){
stack nodeStack;
nodeStack.push(root);
Node *node;
while(!nodeStack.empty()){
node = nodeStack.top();
cout<data;//遍历根结点
nodeStack.pop();
if(node->rchild){
nodeStack.push(node->rchild); //先将右子树压栈
}
if(node->lchild){
nodeStack.push(node->lchild); //再将左子树压栈
}
}
}
广度优先其实就是层序遍历,利用队列很容易实现。
//广度优先遍历
void breadthFirstSearch(Tree root){
queue nodeQueue; //使用C++的STL标准模板库
nodeQueue.push(root);
Node *node;
while(!nodeQueue.empty()){
node = nodeQueue.front();
nodeQueue.pop();
cout<data;//遍历根结点
if(node->lchild){
nodeQueue.push(node->lchild); //先将左子树入队
}
if(node->rchild){
nodeQueue.push(node->rchild); //再将右子树入队
}
}
}
int GetMaxDistance(BiTree &T)//计算二叉树结点的最大距离,递归法
{
if (T == NULL)
return 0;
int Distance = TreeDepth(T->left) + TreeDepth(T->right);
int l_Distance = GetMaxDistance(T->left);
int r_Distance = GetMaxDistance(T->right);
Distance = (Distance > r_Distance) ? Distance : r_Distance;
Distance = (Distance > l_Distance) ? Distance : l_Distance;
return Distance;
}
int CountLeafNode(BiTree &T)//计算二叉树的叶子节点数目,递归法
{
if (T == NULL)
return 0;
if (T->left == NULL&&T->right == NULL)
return 1;
return CountLeafNode(T->left)+CountLeafNode(T->right);
}
//获取二叉树中从根节点到指定节点的路径
void GetNodePath(BiNode* T, BiNode* Node, vector& Path,int &found)
{
if (T == NULL)
return;
Path.push_back(T);
if (T == Node)
found = 1;
if (!found)
{
GetNodePath(T->left, Node, Path, found);
}
if (!found)
{
GetNodePath(T->right, Node, Path, found);
}
if (!found)
Path.pop_back();
else
return;
}
//获取两条路径的最后一个共同节点
BiNode* GetLastCommonNode(const vector Path1, const vector Path2)
{
vector::const_iterator iter1 = Path1.begin();
vector::const_iterator iter2 = Path2.begin();
BiNode *p = NULL;
while ( iter1 != Path1.end() && iter2 != Path2.end() && *iter1 != *iter2 )
{
if (*iter1 == *iter2)
p = *iter1;
iter1++;
iter2++;
}
return p;
}
//获取两个结点的最近共同祖先
BiNode* GetLastCommonParent(BiNode* T, BiNode* Node1, BiNode* Node2)
{
if (T == NULL || Node1 == NULL || Node2 == NULL)
return NULL;
vector Path1, Path2;
int found1 = 0;
int found2 = 0;
GetNodePath(T, Node1, Path1,found1);
GetNodePath(T, Node2, Path2, found2);
return GetLastCommonNode(Path1,Path2);
}