leetcode 102 二叉树的层次遍历 (Binary Tree Level Order Traversal)

我的方法。每个队列保存一层的node:

/**
 * 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>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        queue<TreeNode*> qu;
        if(!root){
            return res;
        }
        qu.push(root);
        soln(res,qu);
        return res;
    }

    void soln(vector<vector<int>>& res, queue<TreeNode*>& qu1){
        if(qu1.empty()){
            return;
        }
        queue<TreeNode*> qu2;
        vector<int> tem;
        while(!qu1.empty()){

            tem.push_back(qu1.front()->val);
            if(qu1.front()->left){
                qu2.push(qu1.front()->left);
            }
            if(qu1.front()->right){
                qu2.push(qu1.front()->right);
            }
            qu1.pop();

        }
        res.push_back(tem);
        soln(res,qu2);

    }

};

队列

可以用递归。

为什么非得一层加完了再加下一层呢?没有必要。

class Solution {
    private:
    vector<vector<int>>res;
public:
  void levelOrder(struct TreeNode* root,int level){
     if(root==NULL)return ;
     if(level==res.size()){
     vector<int> v;
     res.push_back(v);
     }
     res[level].push_back(root->val);
     levelOrder(root->left,level+1);
     levelOrder(root->right,level+1);
  }
    vector<vector<int>> levelOrder(TreeNode* root) {
      levelOrder(root,0);
        return res;
    }
};  

递归

原文地址:https://www.cnblogs.com/azureice/p/leetcode102.html

时间: 2024-11-02 17:54:40

leetcode 102 二叉树的层次遍历 (Binary Tree Level Order Traversal)的相关文章

[Swift]LeetCode102. 二叉树的层次遍历 | 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,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]

【LeetCode从零单排】No102 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] ] 基本二

二叉树叶子顺序遍历 &#183; binary tree leaves order traversal

[抄题]: 给定一个二叉树,像这样收集树节点:收集并移除所有叶子,重复,直到树为空. 给出一个二叉树: 1 / 2 3 / \ 4 5 返回 [[4, 5, 3], [2], [1]]. [暴力解法]: 时间分析: 空间分析: [思维问题]: 觉得要用BFS [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷]: [五刷]: [五分钟肉眼debug的结果]: [总结]: [复杂

leetcode—102 Binary Tree Level Order Traversal(二叉树的层次遍历)

Binary Tree Level Order Traversal Total Accepted: 51429 Total Submissions: 174478 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},

LeetCode 102 Binary Tree Level Order Traversal(二叉树的层级顺序遍历)(*)

翻译 给定一个二叉树,返回按层级顺序遍历的每个节点的值. 从左到右,逐层遍历. 例如: 给定一个二叉树 {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 返回它的层级顺序遍历结果为: [ [3], [9,20], [15,7] ] 翻译 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For

leetCode 107. Binary Tree Level Order Traversal II 二叉树层次遍历反转

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

leetcode 题解: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] ] co

LeetCode: Binary Tree Level Order Traversal 层序遍历二叉树

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 trav

leetCode102. Binary Tree Level Order Traversal 二叉树层次遍历

102. 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,null,null,15,7],     3    /   9  20     /     15   7 re