二叉树的打印

1、层序打印:
用一个队列存储节点即可。需要注意的是 如何判断当前层 节点访问结束,代码中用一个int型变量存储当前层的节点数。

/*
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> >ans;
            if (pRoot==nullptr)return ans;
            
             vector<int>res;
            int toBePrint = 1;
            int next = 0;
            
            queue<TreeNode* >q;
            q.push(pRoot);
            while(!q.empty())
            {
                TreeNode* tmp = q.front();
                q.pop();
                res.push_back(tmp->val);
                toBePrint--;
                
                if (tmp->left)
                {
                    q.push(tmp->left);
                    next++;
                }
                if (tmp->right)
                {
                    q.push(tmp->right);
                    next++;
                }
                
                if (toBePrint==0)
                {
                    toBePrint = next;
                    next = 0;
                    ans.push_back(res);
                    res.clear();
                }
            }
            return ans;
        }
    
};

2. 之字形打印二叉树

即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

观察规律,可以发现,一、三、五……行的节点入栈的顺序都是先右后左;二、四、六……反之。

用两个栈存储节点,一个栈存储当前要访问的节点,另一个栈存储当前节点的下一层节点。

两个变量 cur, next在1,0之间变动,模拟当前层和下一层栈的情况。

#include<iostream>
#include<stack> 
#include<vector>
using namespace std;


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> >ans;
        if (pRoot==nullptr)return ans;
        stack<TreeNode* >node[2];
        int cur = 0;
        int next = 1;
        
        node[cur].push(pRoot);
        vector<int>res;
        while(!node[0].empty() || !node[1].empty())
        {
            TreeNode* tmp = node[cur].top();
            node[cur].pop();
            int num = tmp->val;
            res.push_back(num);
            
            if (cur==0)
            {
                if(tmp->left)node[next].push(tmp->left);
                if(tmp->right)node[next].push(tmp->right);
            }
            else{
                if (tmp->right)node[next].push(tmp->right);
                if (tmp->left)node[next].push(tmp->left);
            }
            
            if (node[cur].empty())
            {
                cur = 1-cur;
                next = 1-next;
                ans.push_back(res);
                res.clear();
            }
            
        }
        
    }
    
};

int main()
{
	TreeNode tree[7];

    tree[0].val = 8;
    tree[0].left = &tree[1];
    tree[0].right = &tree[2];


    tree[1].val = 6;
    tree[1].left = &tree[3];
    tree[1].right = &tree[4];


    tree[2].val = 10;
    tree[2].left = &tree[5];
    tree[2].right = &tree[6];


    tree[3].val = 5;
    tree[3].left = NULL;
    tree[3].right = NULL;

    tree[4].val = 7;
    tree[4].left = NULL;
    tree[4].right = NULL;

    tree[5].val = 9;
    tree[5].left = NULL;
    tree[5].right = NULL;

    tree[6].val = 11;
    tree[6].left = NULL;
    tree[6].right = NULL;

    Solution solu;
    vector< vector<int> > res = solu.Print(tree);
    for(int i = 0; i < res.size( ); i++)
    {
        for(int j = 0; j < res[i].size( ); j++)
        {
            cout <<res[i][j];
        }
        cout <<endl;
    }

	return 0;
} 

猜你喜欢

转载自blog.csdn.net/xnmc2014/article/details/87437607