LeetCode--513.找树左下角的值(二叉树)

题目描述

给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。

假设二叉树中至少有一个节点。

示例 1:

复制代码
输入: root = [2,1,3]
输出: 1

示例 2:

复制代码
输入: [1,2,3,4,null,5,6,null,null,7]
输出: 7

提示:

  • 二叉树的节点个数的范围是 [1,10^4]
  • -231 <= Node.val <= 231 - 1

代码

迭代法

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    // 迭代法 层序遍历
    public int findBottomLeftValue(TreeNode root) {
        int result = root.val;
        if(root.left==null && root.right==null) return result;
        // 分层存放
        Queue<TreeNode> que = new ArrayDeque<>();
        que.add(root);
        while(!que.isEmpty()){
            int size = que.size();
            result = que.peek().val;
            for(int i=0; i<size; i++){
                TreeNode node = que.poll();
                if(node.left != null) que.add(node.left);
                if(node.right != null) que.add(node.right);
            }
        }
        return result;
    }
}

递归法

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    public int result;

    public int maxDepth=0;

    public void traverse(TreeNode node, int depth){
        if(node==null) return;
        // 前序遍历 先处理中间节点
        if(depth>maxDepth && node.left == null && node.right ==null){
            maxDepth = depth;
            result = node.val;
            return;
        }
        // 处理左节点
        if(node.left != null){
            depth++;
            traverse(node.left, depth);
            depth--; //回溯
        }
        // 处理右节点
        if(node.right != null){
            depth++;
            traverse(node.right, depth);
            depth--; //回溯
        }
        return;
    }

    public int findBottomLeftValue(TreeNode root) {
        traverse(root, 1);
        return result;
    }
}
相关推荐
小王C语言5 分钟前
优选算法:队列+宽选(bfs)
数据结构·算法·宽度优先
darkb1rd39 分钟前
WPS 每日任务自动化:签到、抽奖和试用一次配好
java·自动化·wps
AI殉道师1 小时前
合因科技三大临床试验数字化模块:EDC + RTSM(IWRS)+ ePRO,从入组到随访如何协同?
科技·算法·临床试验数字化
夜雪轻寒1 小时前
AI 工具踩坑录:Prompt 我越写越长,模型反而越来越笨,删到第三版才明白它在装
java·开发语言·人工智能·语言模型·自然语言处理·prompt
Helix2501 小时前
JDK IO流与文件操作NIO.2实战:Files/Path/InputStream/OutputStream读写完整教程
java·jdk·文件读写·inputstream·java io·files·nio.2
nice_lcj5201 小时前
在 Spring Boot 里学会使用 RabbitMQ:从入门到实战
java·spring boot·spring·rabbitmq·java-rabbitmq
安易算力1 小时前
大模型推理部署实战:vLLM与TensorRT-LLM配置参数深度解析
java·spring·vllm
风中芦苇啊2 小时前
Spring AI 项目实战:从零搭建一个智能问答服务
java·人工智能·spring
Shaoshing2 小时前
LeetCode100 76.最小覆盖字串
java·算法·leetcode
这个昵称不容易2 小时前
SpringBoot 房屋租赁系统设计与实现:160 个 Java 文件、205 个接口,附真实运行截图
java·springboot·毕设