# 107.Binary Tree Level Order Traversal II

难度:Easy

**107.Binary Tree Level Order Traversal II**

> 给定一个二叉树，返回其节点值自底向上的层次遍历。 （即按从叶子节点所在层到根节点所在的层，逐层从左向右遍历）

```
例如：
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回其自底向上的层次遍历为：

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

类似于[429题](/leetcode/429.n_ary_tree_level_order_traversal.md)的N叉树的层次遍历。代码如下:

```
/**
 * 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) {
        deque<TreeNode*> dt;
        vector<vector<int>> res;
        if(!root) return res;
        dt.push_back(root);
        while(!dt.empty())
        {
            vector<int> tmp;
            for(auto i:dt)
                tmp.push_back(i->val);
            int len=dt.size();
            while(len--){
            if(dt[0]->left)
                dt.push_back(dt[0]->left);  
            if(dt[0]->right)
                dt.push_back(dt[0]->right);
            dt.pop_front();
            }
            // cout<<dt.size()<<endl;
            

            res.push_back(tmp);
            
           
        }
        reverse(res.begin(),res.end());
        return res;
        
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/107.binary_tree_level_order_traversal_ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
