> For the complete documentation index, see [llms.txt](https://dfine.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dfine.gitbook.io/leetcode/107.binary_tree_level_order_traversal_ii.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
