数据结构之二叉树基础三

#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <stack>
#include <vector>
#include <queue>
#include <cmath>

using namespace std;

struct BTNode
{
    int val;
    BTNode *lc;
    BTNode *rc;
};
typedef struct BTNode;



//重建二叉树
/*
1、用前序序列的第一个结点作为根结点;
2、在中序序列中查找根结点的位置,并以此为界将中序序列划分为左、右两个序列(左、右子树);
3、根据左、右子树的中序序列中的结点个数,将前序序列去掉根结点后的序列划分为左、右两个序列,它们分别是左、右子树的前序序列;
4、对左、右子树的前序序列和中序序列递归地实施同样方法,直到所得左、右子树为空。
*/
BTNode* Construct(int a[],int n,int b[],int m)
{
    //由前序遍历和中序遍历重建二叉树
}

//判断一棵二叉树是否为完全二叉树
bool isCompleteBT(BTNode*root)
{
    queue<BTNode*> q;
    BTNode* p;
    q.push(root);
    // 进行广度优先遍历(层次遍历),并把NULL节点也放入队列
    while((p=q.front())!=NULL)
    {
        q.pop();
        q.push(p->lc);
        q.push(p->rc);
    }
    //判断是否有未被访问到的结点
    while(!q.empty())
    {
        p=q.front();
        q.pop();
        if(p!=NULL)//未被访问的非空结点
        {
            return false;
        }
    }
    return true;
}




int depth(BTNode* root)
{
    if(root==NULL)
    {
        return 0;
    }
    int n1,n2;
    n1=depth(root->lc);
    n2=depth(root->lc);
    return (n1>n2)?1+n1:1+n2;
}



//找出二叉树中最远结点的距离
int maxPath=0;
int longestPath(BTNode*root)
{
    int temp;
    if(root!=NULL)
    {
        temp=depth(root->lc)+depth(root->rc);
        if(temp>maxPath)maxPath=temp;
    }
    if(root->lc!=NULL)
        longestPath(root->lc);//递归查找左子树
    if(root->rc!=NULL)
        longestPath(root->rc);//递归查找右子树
    return maxPath;
}


//二叉树中和为某一值的路径
void findPath(BTNode*pTreeNode,int expectedSum,vector<int> path,int currentSum)
{
    if(!pTreeNode)
        return;
    currentSum += pTreeNode->val;//结点值添加至当前和变量中
    path.push_back(pTreeNode->val);//结点压入栈中
    bool isLeaf = !(pTreeNode->lc) && !(pTreeNode->rc);//当前结点为叶子结点
    if(isLeaf&¤tSum == expectedSum)//叶子结点,和为期望值,打印路径
    {
        vector<int>::iterator iter;
        for(iter = path.begin(); iter != path.end(); iter++)
        {
            cout << *iter << " ";
        }
        cout << endl;
    }
    if(pTreeNode->lc)//当前结点不为叶子结点,查找它的左右孩子结点
        findPath(pTreeNode->lc, expectedSum, path, currentSum);
    if(pTreeNode->rc)
        findPath(pTreeNode->rc, expectedSum, path, currentSum);
    currentSum -= pTreeNode->val;//左右孩子结束,删除当前结点,退至其父结点
    path.pop_back();
}


//找到二叉树中两个结点最近的公共祖先结点
BTNode* FindNearestAncestor(BTNode*root)
{

}



//层序遍历
vector<vector<int> > levelOrder(BTNode* pRoot)
{
    vector<vector<int> > res;
    if(pRoot == NULL)
        return res;
    queue<BTNode*> Que;
    Que.push(pRoot);
    while(!Que.empty())
    {
        vector<int> vec;
        int size = Que.size();
        for(int i = 0; i < size; i ++)
        {
            BTNode* temp = Que.front();
            vec.push_back(temp->val);
            if(temp->lc)
                Que.push(temp->lc);
            if(temp->rc)
                Que.push(temp->rc);
            Que.pop();
        }
        res.push_back(vec);
    }
    return res;
}



int main()
{
    return 0;
}

原文地址:https://www.cnblogs.com/jasonhaven/p/7355038.html