现在的位置: 首页 > 综合 > 正文

LeetCode题解:Binary Tree Level Order Traversal I and II

2019年07月25日 ⁄ 综合 ⁄ 共 2219字 ⁄ 字号 评论关闭

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

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

思路:

把栈式的访问改成队列式的访问。深度优先搜索改为广度优先搜索。

题解:

/**
 * 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> > levelOrder(TreeNode *root) {
        if (!root)
            return vector<vector<int>>();
        
        queue<pair<TreeNode*, int>> visit_list;
        vector<vector<int>> ret;
        
        visit_list.push(make_pair(root, 0));
        while(!visit_list.empty())
        {
            while(visit_list.front().second >= ret.size())
                ret.push_back(vector<int>());
            ret[visit_list.front().second].push_back(visit_list.front().first->val);
            
            if (visit_list.front().first->left != nullptr)
                visit_list.push(make_pair(visit_list.front().first->left, visit_list.front().second + 1));
            if (visit_list.front().first->right != nullptr)
                visit_list.push(make_pair(visit_list.front().first->right, visit_list.front().second + 1));
            visit_list.pop();
        }
        
        return ret;
    }
};

思路:

同上,只不过多一次std::reverse的工作。注意这里用reverse并不会带来vector的值交换,而只是交换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> > levelOrderBottom(TreeNode *root) {
        vector<vector<int>> retval;
        if (root == nullptr)
            return retval;
        
        queue<pair<TreeNode*, int>> traverse;
        traverse.push(make_pair(root, 1));
        while(!traverse.empty())
        {
            auto front = traverse.front();
            traverse.pop();
            
            if (retval.size() < front.second)
                retval.push_back(vector<int>());
            retval.back().push_back(front.first->val);
            
            if (front.first->left !=  nullptr)
                traverse.push(make_pair(front.first->left, front.second + 1));
            if (front.first->right != nullptr)
                traverse.push(make_pair(front.first->right, front.second + 1));
        }
        
        // since std::swap is used 
        // there is actually no vector swap, just pointers swap
        reverse(begin(retval), end(retval));
        
        return retval;
    }
};

抱歉!评论已关闭.