zoukankan      html  css  js  c++  java
  • Leetcode 103. Binary Tree Zigzag Level Order Traversal

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
    13         vector<int> row;
    14         vector<vector<int>> v;
    15         queue<TreeNode *> q;
    16         if(root == NULL)
    17             return v;
    18         q.push(root);
    19         TreeNode *temp;
    20         int lev = 0;
    21         while(!q.empty()) {
    22             int size = q.size();
    23             while(size--) {
    24                 temp = q.front();
    25                 q.pop();
    26                 row.push_back(temp->val);
    27                 if(temp->left != NULL) {
    28                     q.push(temp->left);
    29                 }
    30                 if(temp->right != NULL) {
    31                     q.push(temp->right);
    32                 }
    33             }
    34             if(lev % 2) {
    35                 int n = row.size();
    36                 for(int i = 0; i < n/2; i++) {
    37                     swap(row[i], row[n-i-1]);
    38                 }
    39             }
    40             v.push_back(row);
    41             lev++;
    42             row.clear();
    43         }
    44         return v;
    45     }
    46 };
    View Code

    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]
    ]
    

    分析:和层序遍历一样的代码,只需要加几行代码就行~~~因为要之字型存储这个二叉树~~~
    所以只不过在行数为双数的时候需要对当前行进行一次所有元素的倒置~可以用stack也可以用数组头尾两两交换的方法~~
    只需要在存入二维数组vector<vector> v之前倒置好row数组,再push_back到v里面就行~
     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
    13         vector<int> row;
    14         vector<vector<int>> v;
    15         queue<TreeNode *> q;
    16         if(root == NULL)
    17             return v;
    18         q.push(root);
    19         TreeNode *temp;
    20         int lev = 0;
    21         while(!q.empty()) {
    22             int size = q.size();
    23             while(size--) {
    24                 temp = q.front();
    25                 q.pop();
    26                 row.push_back(temp->val);
    27                 if(temp->left != NULL) {
    28                     q.push(temp->left);
    29                 }
    30                 if(temp->right != NULL) {
    31                     q.push(temp->right);
    32                 }
    33             }
    34             if(lev % 2) {
    35                 int n = row.size();
    36                 for(int i = 0; i < n/2; i++) {
    37                     swap(row[i], row[n-i-1]);
    38                 }
    39             }
    40             v.push_back(row);
    41             lev++;
    42             row.clear();
    43         }
    44         return v;
    45     }
    46 };
    View Code
    
    
    
    
    
  • 相关阅读:
    用PHPMailer在本地win环境,可以接收到邮件和附件,但在linux环境只能接收邮件信息接不到附件,是我的路
    linux 下 用phpmailer类smtp发送邮件始终不成功,提示:ERROR: Failed to co
    linux 下 用phpmailer类smtp发送邮件始终不成功,提示:ERROR: Failed to co
    phpmailer的SMTP ERROR: Failed to connect to server: 10
    SDK是什么?什么是SDK
    查看php的配置文件Php.ini的位置
    紧急求助!配置SMTP插件出错,SMTP connect() failed
    PHP move_uploaded_file() 函数
    HTML 5 video 视频标签全属性详解
    RAID级别与规范
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5503432.html
Copyright © 2011-2022 走看看