剑指32:从上到下打印二叉树

按层遍历二叉树 并不分行打印

方法1: 用双向队列辅助

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<int> PrintFromTopToBottom(TreeNode* root) {
        vector<int> result;
        if(root == nullptr)
            return result;
        deque<TreeNode*> sup;
        sup.push_back(root);
        while(sup.size()){
            TreeNode* pNode = sup.front();
            result.push_back(pNode->val);
            sup.pop_front();
            if(pNode->left)
                sup.push_back(pNode->left);
            if(pNode->right)
                sup.push_back(pNode->right);
        }
        return result;
    }
};

方法2:单向队列

            入队不判空出队判空的缺点是需要进行多次出队判空操作 优点是代码简洁

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<int> PrintFromTopToBottom(TreeNode *rt) {
        queue<TreeNode*> q;
        q.push(rt);
        vector<int> r;
        while(!q.empty()){
            rt = q.front(); 
            q.pop();
            //出队时判空
            if(!rt) continue;
            //非空则插入动态数组
            r.push_back(rt -> val);
            //对入队列不进行判空 
            q.push(rt -> left);
            q.push(rt -> right);
        }
        return r;
    }
};

按层遍历并分行打印

第一种 入不看空 出判空

void Printabc(BinaryTreeNode* pRoot){
    std::queue<BinaryTreeNode*> q;
    q.push(pRoot);
    int curline=1;
    int nextline=0;
    while(q.size()){
        pRoot = q.front();
        q.pop();
        curline--;
        if(!pRoot){
            if(curline==0)
            {
                std::cout<<'\n';
                curline=nextline;
                nextline = 0;
            }
            continue;
        }
        std::cout << pRoot->m_nValue;
        q.push(pRoot->m_pLeft);
        q.push(pRoot->m_pRight);
        nextline+=2;
        if(curline==0)
        {
            std::cout<<'\n';                                                                                                                                                                                           
            curline=nextline;
            nextline = 0;
        }
    }
    return;
}
//入判空
void Printo(BinaryTreeNode* pRoot){
    if(pRoot == nullptr)
        return;
    std::queue<BinaryTreeNode*> q;
    q.push(pRoot);
    int curline = 1;
    int nextline = 0;
    while(q.size())
    {
        pRoot = q.front();
        q.pop();
        curline--;
        std::cout<<pRoot->m_nValue;
        if(pRoot->m_pLeft)
        {
            nextline++;
            q.push(pRoot->m_pLeft);
        }
        if(pRoot->m_pRight)
        {
            nextline++;
            q.push(pRoot->m_pRight);
        }
        if(curline==0){
            std::cout<<'\n';
            curline = nextline;
            nextline = 0;
        }
    }
    return;
}

按层遍历二叉树 按行打印 Z字打印 从左到右 从右到左交叉

2个栈

void Printabc(BinaryTreeNode* pRoot){
    if(pRoot == nullptr)
        return;
    std::stack<BinaryTreeNode*> sup[2];
    int current = 0;
    int next = 1;
    sup[current].push(pRoot);
    while(sup[0].size()>0||sup[1].size()>0){
        pRoot=sup[current].top();
        sup[current].pop();
        std::cout<<pRoot->m_nValue<<' ';
        if(current==1) //从右子树到左子树
        {
            if(pRoot->m_pRight)
                sup[next].push(pRoot->m_pRight);
            if(pRoot->m_pLeft)
                sup[next].push(pRoot->m_pLeft);
        }
        else//从左子树到右子树
        {

            if(pRoot->m_pLeft)
                sup[next].push(pRoot->m_pLeft);                                                                                                                                             
            if(pRoot->m_pRight)
                sup[next].push(pRoot->m_pRight);
        }
        if(sup[current].empty()){
            current = 1- current;
            next = 1-next;
            std::cout<<'\n';
        }
    }
    return;

}
/**********************之字形打印二叉树********************************/
/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int> > res;
        if(!pRoot)
            return res;
        stack<TreeNode* > sup[2];
        int current = 1;
        int next = 0;
        sup[current].push(pRoot);
        
        while( sup[0].size()||sup[1].size())
        {
            vector<int> tmp;
            while(sup[current].size())
            {
                pRoot = sup[current].top();
                sup[current].pop();
                tmp.push_back(pRoot->val);
                if(current)  //right to left
                {
                    if(pRoot->left)
                        sup[next].push(pRoot->left);
                    if(pRoot->right)
                        sup[next].push(pRoot->right);
                }
                else 
                {
                    if(pRoot->right)
                        sup[next].push(pRoot->right);
                    if(pRoot->left)
                        sup[next].push(pRoot->left);
                }
            }
            res.push_back(tmp);
            current = 1 - current;
            next = 1- next;
        }
     return res;   
    }
    
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值