在codewars里升级吧~5:一道5级题代码及讲解视频

我在codewars里讲的东西说不定会在大项目里重复,但是做大项目得到的东西远远要比练算法题要多的多。还是做好再拖几期的准备哦, 准时间是不可能说的,说完又要鸽了。

今天讲的还是一道5级题。刚做出来时,觉得只有4行代码,太简单没有什么好讲的,于是决定顺便讲讲别人都是如何解这道题的。

腾讯讲解视频链接

https://v.qq.com/x/page/c0733lw3d2z.html

b站讲解视频链接

https://www.bilibili.com/video/av27620749

题  目

You are given a non-null array of integers. Implement the method array To Tree which creates a binary tree from its values in accordance to their order, while creating nodes by depth from left to right.

SOLUTION

 class Solution {
public:
    static TreeNode* ArrayToTree_R(std::vector<int>& arr, int pointer) {
        if (arr.size() <= pointer) return nullptr;
        return new TreeNode(arr[pointer], ArrayToTree_R(arr, pointer * 2 + 1), ArrayToTree_R(arr, pointer * 2 + 2));
        }
    static TreeNode* arrayToTree(std::vector<int> arr) {
        return ArrayToTree_R(arr, 0);
    }
};

猜你喜欢

转载自blog.csdn.net/Huocoding/article/details/81194680
今日推荐