leetcode 513

Given a binary tree, find the leftmost value in the last row of the tree.

Example 1:

Input:

    2
   / \
  1   3

Output:
1

Example 2: 

Input:

        1
       / \
      2   3
     /   / \
    4   5   6
       /
      7

Output:
7

Note: You may assume the tree (i.e., the given root node) is not NULL.

Subscribe to see which companies asked this question.

dfs

设置两个全局变量,深度、res 注意深度的加减,

代码:

/**
 * 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:
    int depth = 0;
    int max_depth = 0;
    int val;
    int findBottomLeftValue(TreeNode* root) {
        if (root -> left == NULL && root -> right == NULL)
        {
            depth += 1;
            if(depth > max_depth)
            {
                val = root -> val;
                max_depth = depth;
            }
            depth -= 1;
            return val;
        }
        
        if(root->left !=  NULL)
        {
            depth += 1;
            findBottomLeftValue(root->left);
            depth -= 1;
        }
            
        if(root->right != NULL)
        {
            depth += 1;
            findBottomLeftValue(root->right);
            depth -= 1;
        }
            
        return val;
    }
};

### LeetCode编程题解和资源 #### 关于LeetCode平台概述 LeetCode是一个在线刷题网站,专注于帮助开发者准备技术面试。该平台上提供了大量的算法题目以及数据库相关问题,这些问题被广泛用于各大科技公司的面试环节中[^1]。 #### 获取官方文档和支持材料 对于希望深入理解每道题目的解答思路而言,最直接的方式就是利用LeetCode官方网站所提供的解释说明。每当完成一道练习之后,在页面下方可以找到详细的解析文章,这些资料由社区成员共同维护更新,质量较高且覆盖面广[^2]。 #### 利用开源项目学习 GitHub上存在许多优秀的开源仓库专门整理了针对LeetCode各类难题的解决方案集合。例如,“leetcode-solution”这个库就包含了几乎所有热门试题的标准答案及其背后原理分析;还有像“algorithm-patterns-for-coding-interviews”,它不仅给出了具体实现方法还总结了一些常见模式以便更好地应对不同类型的考题[^3]。 #### 加入讨论区交流心得 除了阅读现成的答案之外,积极参与到LeetCode论坛当中也是一个不错的选择。在这里能够与其他程序员分享自己的思考过程并获得反馈意见,从而加深对知识点的记忆程度。同时也可以向他人请教那些自己难以解决的问题,通过互动促进彼此成长进步[^4]。 ```python # 示例:如何在Python环境中调用LeetCode API获取每日一题信息 import requests def get_daily_problem(): url = "https://leetcode.com/graphql" query = ''' { activeDailyCodingChallengeQuestion { date question { titleSlug frontendQuestionId: questionFrontendId difficulty } } } ''' response = requests.post(url, json={'query': query}) data = response.json() daily_question_info = data['data']['activeDailyCodingChallengeQuestion'] return f"Today's Daily Problem is {daily_question_info['question']['frontendQuestionId']} - {daily_question_info['question']['titleSlug']}, Difficulty Level:{daily_question_info['question']['difficulty']}" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值