程序员面试金典 - 面试题 04.12. 求和路径(二叉树递归)

本文介绍了一种算法,用于解决给定二叉树中所有路径数值总和等于特定值的问题。通过递归方法,从每个节点开始计算路径和,实现了路径数量的有效统计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 题目

给定一棵二叉树,其中每个节点都含有一个整数数值(该值或正或负)。

设计一个算法,打印节点数值总和等于某个给定值的所有路径的数量。

注意,路径不一定非得从二叉树的根节点或叶节点开始或结束,但是其方向必须向下(只能从父节点指向子节点方向)。

示例:
给定如下二叉树,以及目标和 sum = 225
             / \
            4   8
           /   / \
          11  13  4
         /  \    / \
        7    2  5   1
返回:

3
解释:和为 22 的路径有:[5,4,11,2], [5,8,4,5], [4,11,7]

提示:
节点总数 <= 10000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/paths-with-sum-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 遍历每个节点,并从该节点开始计算和
class Solution {
    int count = 0;
public:
    int pathSum(TreeNode* root, int sum) {
        if(!root) return 0;
        calsum(root,0,sum);
        pathSum(root->left,sum);
        pathSum(root->right,sum);
        return count;
    }

    void calsum(TreeNode* root, int s, int sum)
    {
        if(!root)  
            return;
        if(s+root->val == sum)
            count++;
        calsum(root->left,s+root->val,sum);
        calsum(root->right,s+root->val,sum);
    }
};

在这里插入图片描述

using UnityEngine; using UnityEngine.Video; using UnityEngine.UI; [RequireComponent(typeof(RawImage))] public class TransparentWebMPlayer : MonoBehaviour { [Header("视频设置")] [Tooltip("StreamingAssets文件夹中的WebM文件名(不带扩展名)")] public string videoFileName = "transparent"; [Tooltip("视频是否循环播放")] public bool loop = true; [Header("性能设置")] [Range(0.1f, 2f)] public float playbackSpeed = 1.0f; private VideoPlayer videoPlayer; private RawImage rawImage; private RenderTexture renderTexture; private Material transparentMaterial; void Start() { // 创建透明材质 CreateTransparentMaterial(); // 初始化组件 rawImage = GetComponent<RawImage>(); videoPlayer = gameObject.AddComponent<VideoPlayer>(); // 设置视频源 SetupVideoSource(); // 配置VideoPlayer ConfigureVideoPlayer(); // 准备视频 videoPlayer.Prepare(); } void CreateTransparentMaterial() { // 创建支持透明通道的自定义材质 transparentMaterial = new Material(Shader.Find("UI/Unlit/Transparent")); transparentMaterial.name = "WebM_Transparent_Material"; } void SetupVideoSource() { // 构建文件路径 string filePath = System.IO.Path.Combine( Application.streamingAssetsPath, videoFileName + ".webm" ); // 设置视频源 videoPlayer.source = VideoSource.Url; videoPlayer.url = filePath; // 特殊平台处理 #if UNITY_ANDROID || UNITY_IOS videoPlayer.url = "file://" + filePath; #endif } void ConfigureVideoPlayer() { // 创建RenderTexture renderTexture = new RenderTexture(1920, 1080, 0, RenderTextureFormat.ARGB32); renderTexture.name = "WebM_RenderTexture"; // 配置VideoPlayer videoPlayer.renderMode = VideoRenderMode.RenderTexture; videoPlayer.targetTexture = renderTexture; videoPlayer.isLooping = loop; videoPlayer.playbackSpeed = playbackSpeed; videoPlayer.audioOutputMode = VideoAudioOutputMode.None; // 禁用音频 // 设置事件回调 videoPlayer.prepareCompleted += OnVideoPrepared; videoPlayer.errorReceived += OnVideoError; } void OnVideoPrepared(VideoPlayer source) { // 应用材质到UI rawImage.material = transparentMaterial; rawImage.texture = renderTexture; // 开始播放 videoPlayer.Play(); Debug.Log($"开始播放透明视频: {videoFileName}"); } void OnVideoError(VideoPlayer source, string message) { Debug.LogError($"视频播放错误: {message}"); // 尝试使用备用视频 if (videoFileName != "backup") { videoFileName = "backup"; SetupVideoSource(); videoPlayer.Prepare(); } } void OnDestroy() { // 清理资源 if (renderTexture != null) { renderTexture.Release(); Destroy(renderTexture); } if (videoPlayer != null) { videoPlayer.Stop(); Destroy(videoPlayer); } } // 外部控制方法 public void Play() => videoPlayer?.Play(); public void Pause() => videoPlayer?.Pause(); public void Stop() => videoPlayer?.Stop(); public void SetPlaybackSpeed(float speed) { if (videoPlayer != null) { videoPlayer.playbackSpeed = Mathf.Clamp(speed, 0.1f, 2f); } } } 加一下把MP4转换为wbem vp8类型的
最新发布
07-20
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Michael阿明

如果可以,请点赞留言支持我哦!

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

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

打赏作者

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

抵扣说明:

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

余额充值