Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

 

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]

C++实现如下:

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

//Definition for binary tree
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution
{
public:
    vector<vector<int> > zigzagLevelOrder(TreeNode *root)
    {
        stack<int> st;
        vector<vector<int> > vec;//用来存放每一层的关键字
        vector<vector<TreeNode*> > rvec; //用来存放每一层的结点
        size_t i=0;
        if(root==NULL)
            return vector<vector<int> >();
        vec.push_back({root->val});
        rvec.push_back({root});
        vector<int> v1; //用来存放一层的关键字
        vector<TreeNode*> v2; //用来存放一层的结点
        int flag=0;//控制是正序还是逆序打印该层的元素
        while(rvec.size())
        {
            v1.clear();
            v2.clear();
            if(flag==0)
            {
                //每次只对最后一层进行操作
                for(i=0; i<rvec[rvec.size()-1].size(); i++)
                {
                    TreeNode *tmp=rvec[rvec.size()-1][i];
                    if(tmp->left)
                    {
                        st.push(tmp->left->val);
                        v2.push_back(tmp->left);
                    }
                    if(tmp->right)
                    {
                        st.push(tmp->right->val);
                        v2.push_back(tmp->right);
                    }
                }
                while(!st.empty())
                {
                    v1.push_back(st.top());
                    st.pop();
                }
                if(!v1.empty()&&!v2.empty())
                {
                    vec.push_back(v1);
                    rvec.push_back(v2);
                }
                else
                    break;
                flag=1;
            }
            else
            {
                for(i=0; i<rvec[rvec.size()-1].size(); i++)
                {
                    TreeNode *tmp=rvec[rvec.size()-1][i];
                    if(tmp->left)
                    {
                        v1.push_back(tmp->left->val);
                        v2.push_back(tmp->left);
                    }
                    if(tmp->right)
                    {
                        v1.push_back(tmp->right->val);
                        v2.push_back(tmp->right);
                    }
                }
                if(!v1.empty()&&!v2.empty())
                {
                    vec.push_back(v1);
                    rvec.push_back(v2);
                }
                else
                    break;
                flag=0;
            }
        }
        return vec;
    }
    void createTree(TreeNode *&root)
    {
        int i;
        cin>>i;
        if(i!=0)
        {
            root=new TreeNode(i);
            if(root==NULL)
                return;
            createTree(root->left);
            createTree(root->right);
        }
    }
};

int main()
{
    Solution s;
    TreeNode *root;
    s.createTree(root);
    vector<vector<int> > vec=s.zigzagLevelOrder(root);
    for(auto a:vec)
    {
        for(auto v:a)
            cout<<v<<" ";
        cout<<endl;
    }
}

运行结果:

时间: 2024-10-27 05:19:14

Binary Tree Zigzag Level Order Traversal的相关文章

[LeetCode]103.Binary Tree Zigzag Level Order Traversal

[题目] Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 retur

[LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

目录:1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次遍历从低往高输出 BFS 3.Maximum Depth of Binary Tree - 求二叉树的深度 DFS4.Balanced Binary Tree - 判断平衡二叉树 DFS5.Path Sum - 二叉树路径求和判断DFS 题目概述:Given a binary tree, return

[LeetCode]102.Binary Tree Level Order Traversal

[题目] Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], [15,7

[LeetCode]--107. Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its bottom-up level o

LeetCode: Binary Tree Level Order Traversal 层序遍历二叉树

 Binary Tree Level Order Traversal  题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level ord

【LeetCode从零单排】No102 Binary Tree Level Order Traversal

题目        Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], [

Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7   return its level order traversal as: [ [3], [9,20], [15,7] ]

Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7   return its bottom-up level order

[LeetCode]94.Binary Tree Inorder Traversal

[题目] Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}" means? &