• 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)


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

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

    confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


    OJ's Binary Tree Serialization:

    The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

    Here's an example:

       1
      / 
     2   3
        /
       4
        
         5
    
    The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
     
    解法一:递归
    参考了Discussion中stellari的做法,递归进行层次遍历,并将每个level对应于相应的vector。
    /**
     * 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> > result;
    
        void levelTra(TreeNode *root, int level)
        {
            if(root == NULL)
                return;
            if(level == result.size())
            {
                vector<int> v;
                result.push_back(v);
            }
            result[level].push_back(root->val);
            levelTra(root->left, level+1);
            levelTra(root->right, level+1);
        }
    
        vector<vector<int> > levelOrderBottom(TreeNode *root) 
        {
            levelTra(root, 0);
            return vector<vector<int> >(result.rbegin(), result.rend());
        }
    };

     

    解法二:普通层次遍历后逆序。

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
     
    struct Node
    {
        TreeNode* tNode;
        int level;
        Node(TreeNode* newtNode, int newlevel): tNode(newtNode), level(newlevel) {}
    };
    
    class Solution {
    public:
        vector<vector<int> > levelOrderBottom(TreeNode *root) {
            vector<vector<int> > ret;
            if(!root)
                return ret;
            // push root 
            Node* rootNode = new Node(root, 0);
            queue<Node*> Nqueue;
            Nqueue.push(rootNode);
            
            vector<int> cur;
            int curlevel = 0;
            while(!Nqueue.empty())
            {
                Node* frontNode = Nqueue.front();
                Nqueue.pop();
                
                if(frontNode->level > curlevel)
                {
                    ret.push_back(cur);
                    cur.clear();
                    curlevel = frontNode->level;
                }
    
                cur.push_back(frontNode->tNode->val);
                
                if(frontNode->tNode->left)
                {
                    Node* leftNode = new Node(frontNode->tNode->left, frontNode->level+1);
                    Nqueue.push(leftNode);
                }
                if(frontNode->tNode->right)
                {
                    Node* rightNode = new Node(frontNode->tNode->right, frontNode->level+1);
                    Nqueue.push(rightNode);
                }
            }
            ret.push_back(cur);
            
            reverse(ret.begin(), ret.end());
            return ret;
        }
    };

  • 相关阅读:
    C#设计模式(8)——桥接模式(Bridge Pattern)
    我的微软最有价值专家(Microsoft MVP)之路
    C#设计模式(7)——适配器模式(Adapter Pattern)
    C#设计模式(6)——原型模式(Prototype Pattern)
    C#设计模式(5)——建造者模式(Builder Pattern)
    C#设计模式(4)——抽象工厂模式
    C#设计模式(3)——工厂方法模式
    C#设计模式(2)——简单工厂模式
    [C# 基础知识系列]C#中易混淆的知识点
    [C# 开发技巧]如何使不符合要求的元素等于离它最近的一个元素
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/3843470.html
Copyright © 2020-2023  润新知