Leetcode P0107"Binary Tree Level Order Traversal II" 题解

2020/09/07 Leetcode

博文中会简要介绍Leetcode P0107题目分析及解题思路。

“Binary Tree Level Order Traversal II”是一道比较基础的树的题目,是p0102的变形题。这道题主要考察的就是广度优先搜索,即BFS。

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,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

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

由于最终结果是从最下层到最上层,我们可以使用栈结构,从上往下存储,最后再出栈存入动态数组结构。由于栈结构本身的特性,先入后出,我们第一个出栈的必定是最后一行的层遍历结果。

以下是Java的题解代码实现。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> levels = new ArrayList<>();
        if (root == null)
            return levels;
        
        Queue<TreeNode> queue = new LinkedList<>();
        Deque<List<Integer>> stack = new LinkedList<>();
        
        List<Integer> level = new ArrayList<>();
        int numOfNodesInCurrLevel = 1, numOfNodesInNextLevel = 0;
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode curr = queue.poll();
            
            if (curr.left != null) {
                ++numOfNodesInNextLevel;
                queue.offer(curr.left);
            }
            if (curr.right != null) {
                ++numOfNodesInNextLevel;
                queue.offer(curr.right);
            }
            
            level.add(curr.val);
            --numOfNodesInCurrLevel;
            if (numOfNodesInCurrLevel == 0) {
                stack.offerFirst(new ArrayList<>(level));
                level.clear();
                
                numOfNodesInCurrLevel = numOfNodesInNextLevel;
                numOfNodesInNextLevel = 0;
            }
        }
        
        while (!stack.isEmpty())
            levels.add(stack.pollFirst());
        
        return levels;
    }
}

以下是C++的题解代码实现。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> levels;
        if (root == NULL)
            return levels;
        
        queue<TreeNode*> node_queue;
        stack<vector<int>> node_stack;
        vector<int> level;
        
        int curr_nodes = 1, next_nodes = 0;
        node_queue.push(root);
        while (!node_queue.empty()) {
            TreeNode *curr = node_queue.front();
            node_queue.pop();
            
            if (curr->left != NULL) {
                ++next_nodes;
                node_queue.push(curr->left);
            }
            if (curr->right != NULL) {
                ++next_nodes;
                node_queue.push(curr->right);
            }
            
            --curr_nodes;
            level.push_back(curr->val);
            if (curr_nodes == 0) {
                node_stack.push(vector<int>(level));
                level.clear();
                
                curr_nodes = next_nodes;
                next_nodes = 0;
            }
        }
        
        while (!node_stack.empty()) {
            levels.push_back(node_stack.top());
            node_stack.pop();
        }
        
        return levels;
    }
};

Search

    Table of Contents