【算法与数据结构】513、LeetCode找树左下角的值

文章目录

所有的LeetCode题解索引,可以看这篇文章------【算法和数据结构】LeetCode题解

一、题目

二、解法

思路分析:这道题用层序遍历来做比较简单,最底层最左边节点就是层序遍历当中最底层元素容器的第一个值,层序遍历利用了【算法和数据结构】102、LeetCode二叉树的层序遍历文章中的迭代法,稍加修改就可以实现题目要求。

程序如下:

cpp 复制代码
// 层序遍历迭代法
class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        int result = 0;
        while (!que.empty()) {
            int size = que.size();  // size必须固定, que.size()是不断变化的
            for (int i = 0; i < size; ++i) {
                TreeNode* node = que.front();
                que.pop();
                if (i == 0) result = node->val; // 访问容器当中第一个元素
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return result;
    }
};

复杂度分析:

  • 时间复杂度: O ( n ) O(n) O(n)。
  • 空间复杂度: O ( n ) O(n) O(n)。

三、完整代码

cpp 复制代码
# include <iostream>
# include <vector>
# include <queue>
# include <string>
using namespace std;

// 树节点定义
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 findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        int result = 0;
        while (!que.empty()) {
            int size = que.size();  // size必须固定, que.size()是不断变化的
            for (int i = 0; i < size; ++i) {
                TreeNode* node = que.front();
                que.pop();
                if (i == 0) result = node->val; // 访问容器当中第一个元素
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return result;
    }
};

void my_print1(vector <string>& v, string msg)
{
    cout << msg << endl;
    for (vector<string>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << "  ";
    }
    cout << endl;
}

void my_print2(vector<vector<int>>& v, string str) {
    cout << str << endl;
    for (vector<vector<int>>::iterator vit = v.begin(); vit < v.end(); ++vit) {
        for (vector<int>::iterator it = (*vit).begin(); it < (*vit).end(); ++it) {
            cout << *it << ' ';
        }
        cout << endl;
    }
}

// 前序遍历递归法创建二叉树,每次迭代将容器首元素弹出(弹出代码还可以再优化)
void Tree_Generator(vector<string>& t, TreeNode*& node) {
    if (t[0] == "NULL" || !t.size()) return;    // 退出条件
    else {
        node = new TreeNode(stoi(t[0].c_str()));    // 中
        t.assign(t.begin() + 1, t.end());
        Tree_Generator(t, node->left);              // 左
        t.assign(t.begin() + 1, t.end());
        Tree_Generator(t, node->right);             // 右
    }
}

int main()
{
    vector<string> t = { "3", "9", "NULL", "NULL", "20", "15", "NULL", "NULL", "7", "NULL", "NULL" };   // 前序遍历
    my_print1(t, "目标树:");
    TreeNode* root = new TreeNode();
    Tree_Generator(t, root);
    Solution s1;
    int result = s1.findBottomLeftValue(root);
    cout << "最底层最左边元素为:  " << result <<endl; 
    system("pause");
    return 0;
}

end

相关推荐
笑口常开xpr1 分钟前
【C++继承】深入浅出C++继承机制
开发语言·数据结构·c++·算法
让我们一起加油好吗1 小时前
【基础算法】DFS
算法·深度优先
爱学习的小鱼gogo2 小时前
python 矩阵中寻找就接近的目标值 (矩阵-中等)含源码(八)
开发语言·经验分享·python·算法·职场和发展·矩阵
红纸2812 小时前
Subword算法之WordPiece、Unigram与SentencePiece
人工智能·python·深度学习·神经网络·算法·机器学习·自然语言处理
CUMT_DJ3 小时前
从零复现论文(1)——通感一体化实现协作基站分配与资源分配(CBARA)策略
算法·通感一体化
tt5555555555553 小时前
CSDN 教程:C++ 经典字符串与栈算法题逐行详解
c++·算法·哈希算法
_dindong4 小时前
基础算法:滑动窗口
数据结构·学习·算法·leetcode·力扣
Voyager_44 小时前
图像处理踩坑:浮点数误差导致的缩放尺寸异常与解决办法
数据结构·图像处理·人工智能·python·算法
文艺倾年4 小时前
【八股消消乐】手撕分布式协议和算法(基础篇)
分布式·算法
万岳科技系统开发5 小时前
从源码优化外卖配送系统:算法调度、智能推荐与数据分析应用
算法·数据挖掘·数据分析