zoukankan      html  css  js  c++  java
  • Binary Tree Vertical Order Traversal -- LeetCode

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bottom, column by column).

    If two nodes are in the same row and column, the order should be from left to right.

    Examples:

     

    Given binary tree [3,9,20,null,null,15,7],

       3
      /
     /  
     9  20
        /
       /  
      15   7

    return its vertical order traversal as:

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

    Given binary tree [3,9,8,4,0,1,7],

         3
        /
       /  
       9   8
      /  /
     /  /  
     4  01   7

    return its vertical order traversal as:

    [
      [4],
      [9],
      [3,0,1],
      [8],
      [7]
    ]

    Given binary tree [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5),

         3
        /
       /  
       9   8
      /  /
     /  /  
     4  01   7
        /
       /  
       5   2

    return its vertical order traversal as:

    [
      [4],
      [9,5],
      [3,0,1],
      [8,2],
      [7]
    ]

    思路:bsf。然后用链表暂时存储结果。假设当前链表节点的iterator为it,则它的前一个节点的iterator可以用std::prev(it, 1)获得,它的下一个节点的iterator可以用std::next(it, 1)获得。

     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>> verticalOrder(TreeNode* root) {
    13         vector<vector<int> > res;
    14         if (root == NULL) return res;
    15         list<vector<int> > columns;
    16         queue<TreeNode*> nodes;
    17         queue<list<vector<int> >::iterator> iterators;
    18         columns.push_back(vector<int>(1, root->val));
    19         nodes.push(root);
    20         iterators.push(columns.begin());
    21         while (!nodes.empty()) {
    22             TreeNode* cur = nodes.front(); nodes.pop();
    23             auto it = iterators.front(); iterators.pop();
    24             if (cur->left) {
    25                 if (it == columns.begin()) columns.push_front(vector<int>(1, cur->left->val));
    26                 else std::prev(it, 1)->push_back(cur->left->val);
    27                 nodes.push(cur->left);
    28                 iterators.push(std::prev(it, 1));
    29             }
    30             if (cur->right) {
    31                 if (std::next(it, 1) == columns.end()) columns.push_back(vector<int>(1, cur->right->val));
    32                 else std::next(it, 1)->push_back(cur->right->val);
    33                 nodes.push(cur->right);
    34                 iterators.push(std::next(it, 1));
    35             }
    36         }
    37         for (auto it = columns.begin(); it != columns.end(); it++)
    38             res.push_back(*it);
    39         return res;
    40     }
    41 };
  • 相关阅读:
    我的JAVA之旅(二)初识JAVA
    ORACLE并发处理
    我的JAVA之旅(一)安装配置
    我的JAVA之旅(三) 元素语法
    改变一生的五句话
    InstallShield集成安装MSDE2000最小版本(三) fishout特许授权发布
    IS2009修改XML File 奕婷特许授权发布
    SQL Server中多个表格求出相同列和不同列(答案来自CSDN上SQL专家的回答)
    InstallShield集成安装MSDE2000最小版本(二) fishout特许授权发布
    Installshield停止操作系统进程的代码 IS6及以上版本适用
  • 原文地址:https://www.cnblogs.com/fenshen371/p/5801658.html
Copyright © 2011-2022 走看看