Leetcode 102. 二叉树的层序遍历(DAY 87) ---- Leetcode Hot 100


原题题目


在这里插入图片描述


代码实现(首刷自解)


/**
 * 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>> levelOrder(TreeNode* root) {
        int count = 0;
        vector<TreeNode*> temp,v;
        vector<vector<int>> ret;
        if(root) temp.push_back(root);
        while(!temp.empty())
        {
            ret.push_back(vector<int>());
            for(const auto& root:temp)
            {
                if(root->left)  v.push_back(root->left);
                if(root->right) v.push_back(root->right);
                ret[count].push_back(root->val);
            }
            temp = v;
            v.clear();
            ++count;
        }
        return ret;
    }
};

代码实现(二刷自解 C++ DAY 149)


/**
 * 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>> levelOrder(TreeNode* root) {
        vector<TreeNode*> next,tmp;
        vector<vector<int>> ret;
        if(!root)   return ret;
        next.emplace_back(root);
        while(next.size())
        {
            vector<TreeNode*> now = next;
            next = tmp;
            vector<int> temp;
            for(const auto& node:now)
            {
                temp.emplace_back(node->val);
                if(node->left)  next.emplace_back(node->left);
                if(node->right) next.emplace_back(node->right);
            }
            ret.emplace_back(temp);
        }
        return ret;
    }
};

代码实现(三刷自解 C++ DAY 201 递归实现)


/**
 * 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:
    int maxheight = -1;

    void depth_first_search(TreeNode* root,vector<vector<int>>& visit,int height)
    {
        if(!root)   return;
        if(height > maxheight)
        {
            visit.emplace_back(vector<int>());
            visit[height].emplace_back(root->val);
        }
        else    visit[height].emplace_back(root->val);
        maxheight = max(maxheight,height);
        depth_first_search(root->left,visit,height+1);
        depth_first_search(root->right,visit,height+1);
    }

    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> visit;
        depth_first_search(root,visit,0);
        return visit;
    }
};

代码实现(四刷自解 DAY 269 层序遍历 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>> levelOrder(TreeNode* root) {
      queue<TreeNode*> queue;
      if (root) {
        queue.emplace(root);
      }

      vector<vector<int>> ret;
      vector<int> tmp;
      while (!queue.empty()) {
        int size = queue.size();
        tmp.clear();
        for (int i = 0; i < size; ++i)
        {
          auto ptr = queue.front();
          queue.pop();
          tmp.emplace_back(ptr->val);
          
          if (ptr->left)  { queue.emplace(ptr->left); }
          if (ptr->right) { queue.emplace(ptr->right); }
        }
        ret.emplace_back(tmp);
      }

      return ret;
    }
};

代码实现(四刷自解 DAY 269 递归实现)


/**
 * 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:
    void DepthFirstSearch(vector<vector<int>>& ret,TreeNode* root,int level) {
      if (!root)  return;
      if (ret.size() < level + 1) {
        ret.emplace_back(vector<int>());
      }

      ret[level].emplace_back(root->val);
      DepthFirstSearch(ret,root->left,level+1);
      DepthFirstSearch(ret,root->right,level+1);

      return;
    }

    vector<vector<int>> levelOrder(TreeNode* root) {
      vector<vector<int>> ret;
      DepthFirstSearch(ret,root,0);

      return ret;
    }
};

代码实现(五刷自解 DAY 2 Golang)


/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func levelOrder(root *TreeNode) [][]int {
  ret := make([][]int, 0)

  if root == nil {
    return ret
  }

  deq := make([]*TreeNode, 0)
  deq = append(deq, root)

  for len(deq) != 0 {
    size := len(deq)
    ret = append(ret, []int{})
    for i := 0; i < size; i++ {
      ptr := deq[i]
      ret[len(ret) - 1] = append(ret[len(ret) - 1], ptr.Val)

      left, right := ptr.Left, ptr.Right
      if left != nil {
        deq = append(deq, left)
      }
      if right != nil {
        deq = append(deq, right)
      }
    }
    deq = deq[size:]
  }
  return ret
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Love 6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值