zoukankan      html  css  js  c++  java
  • 按之字形顺序打印二叉树

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M

    题目描述

    请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

    思路:

    广度优先搜索策略(借助队列),并偶数行进行反转

    /*
    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 == NULL)
                return res;
            queue<TreeNode *> que;
            bool flag = false;//判断是否为偶数行,flag= false为奇数行,flag=true为偶数行
            que.push(pRoot);
            while(!que.empty())
            {
                vector<int> vec;
                int len = que.size();
                for(int i =0;i < len;i++)
                {
                    TreeNode *temp = que.front();
                    vec.push_back(temp->val);
                    que.pop();
                    if(temp->left) que.push(temp->left);
                    if(temp->right) que.push(temp->right);
                }
                if(flag)
                    reverse(vec.begin(),vec.end());
                flag = !flag;
                res.push_back(vec);
            }
            return res;
        }
        
    };
    

    广度优先搜索策略,这里采用两个栈,分别用于储存奇数行和偶数行,实现偶数行从右向左的效果。

    /*
    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*> sta1;//存储奇数行
            stack<TreeNode*> sta2;//存储偶数行
            vector<int> vec;
            sta1.push(pRoot);
            while(!sta1.empty() || !sta2.empty())
            {
                if(!sta1.empty() && sta2.empty())//奇数行不为空
                {
                    int len = sta1.size();
                    for(int i =0;i <len;i++)
                    {
                        TreeNode* temp = sta1.top();
                        vec.push_back(temp->val);
                        sta1.pop();
                        if(temp->left) sta2.push(temp->left);//sta2存放偶数行节点,从左子节点到右子节点压栈
                        if(temp->right) sta2.push(temp->right);//则出栈的效果就是从右向左,符合要求                }
                    }
                    res.push_back(vec);
                    vec.clear();
                }
                if(sta1.empty() && !sta2.empty())//偶数行不为空
                {
                    int len = sta2.size();
                    for(int i =0;i <len;i++)
                    {
                        TreeNode* temp = sta2.top();
                        vec.push_back(temp->val);
                        sta2.pop();
                        if(temp->right) sta1.push(temp->right);//sta1存放奇数行节点,从右节点到左子节点压栈
                        if(temp->left) sta1.push(temp->left);//则出栈的效果就是从左往右符合要求                
                    
                    }
                    res.push_back(vec);
                    vec.clear();
                }
            }
            return res;
        }
    };
    
  • 相关阅读:
    MS SQL Server中的CONVERT日期格式化大全
    简历
    Spring源码IOC部分容器简介【1】
    HadoopHDFS设计原理
    1.Linux系统简介
    3.大话C语言变量和数据类型
    2.C语言初探
    7.函数
    8.C语言预处理命令
    9.指针
  • 原文地址:https://www.cnblogs.com/whiteBear/p/12692803.html
Copyright © 2011-2022 走看看