zoukankan      html  css  js  c++  java
  • 【Binary Tree Level Order Traversal II 】cpp

    题目:

    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 traversal as:

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

    代码:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<vector<int>> levelOrderBottom(TreeNode* root) {
                vector<vector<int> > ret;
                if (!root) return ret;
                vector<int> tmp_ret;
                deque<TreeNode *> currLevel, nextLevel;
                currLevel.push_back(root);
                while ( !currLevel.empty() )
                {
                    while ( !currLevel.empty() )
                    {
                        TreeNode * tmp = currLevel.front();
                        currLevel.pop_front();
                        tmp_ret.push_back(tmp->val);
                        if ( tmp->left ) nextLevel.push_back(tmp->left);
                        if ( tmp->right ) nextLevel.push_back(tmp->right);
                    }
                    ret.push_back(tmp_ret);
                    tmp_ret.clear();
                    std::swap(currLevel, nextLevel);
                }
                std::reverse(ret.begin(), ret.end());
                return ret;
        }
    };

    tips:

    Binary Tree Level Order Traversal的基础上加一个reverse即可。

    ============================================

    第二次过这道题,直接用reverse的路子了。

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<vector<int>> levelOrderBottom(TreeNode* root) {
                vector<vector<int> > ret;
                queue<TreeNode*> curr;
                queue<TreeNode*> next;
                if ( root ) curr.push(root);
                while ( !curr.empty() )
                {
                    vector<int> tmp;
                    while ( !curr.empty() )
                    {
                        tmp.push_back(curr.front()->val);
                        if ( curr.front()->left ) next.push(curr.front()->left);
                        if ( curr.front()->right ) next.push(curr.front()->right);
                        curr.pop();
                    }
                    ret.push_back(tmp);
                    std::swap(next, curr);
                }
                std::reverse(ret.begin(), ret.end());
                return ret;
        }
    };
  • 相关阅读:
    Activity传递数据
    Java JDK环境变量配置
    Java与IOS日期格式
    第十四篇 ANDROID的 BLUETOOTH 实现机制--中介模式和代理模式
    第十五篇 Android 的Backup服务管理机制--助手模式
    第十八篇 ANDROID的声音管理系统及服务
    使用Unsafe来实现自定义锁
    rabbitMQ实现推迟队列
    简单分布式锁的实现
    [原创]通过切面与分布式锁实现合并相同请求
  • 原文地址:https://www.cnblogs.com/xbf9xbf/p/4502623.html
Copyright © 2011-2022 走看看