首页 > 编程语言 >LeetCode 1315. Sum of Nodes with Even-Valued Grandparent

LeetCode 1315. Sum of Nodes with Even-Valued Grandparent

时间:2024-04-17 12:56:22浏览次数:14  
标签:Even 1315 Valued TreeNode val int grandparent nodes root

原题链接在这里:https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/description/

题目:

Given the root of a binary tree, return the sum of values of nodes with an even-valued grandparent. If there are no nodes with an even-valued grandparent, return 0.

A grandparent of a node is the parent of its parent if it exists.

Example 1:

Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
Output: 18
Explanation: The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.

Example 2:

Input: root = [1]
Output: 0

Constraints:

  • The number of nodes in the tree is in the range [1, 104].
  • 1 <= Node.val <= 100

题解:

How does a node know its grandparent, we can pass in the parent and grandparent in the dfs call.

DFS returns the sum of subtree value whose grandparent is even.

Time Complexity: O(n).

Space: O(logn). stack space.

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode() {}
 8  *     TreeNode(int val) { this.val = val; }
 9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     public int sumEvenGrandparent(TreeNode root) {
18         return dfs(root, 1, 1);
19     }
20 
21     private int dfs(TreeNode root, int p, int gp){
22         if(root == null){
23             return 0;
24         }
25 
26         return (gp % 2 == 0 ? root.val : 0) + dfs(root.left, root.val, p) + dfs(root.right, root.val, p);
27     }
28 }

 

标签:Even,1315,Valued,TreeNode,val,int,grandparent,nodes,root
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18140327

相关文章

  • iOS中使用text/event-stream数据流实现后端SSE数据推送
    最近在做通过http请求实现后端一条一条一条消息推送,达到gpt那种搜索的展示的效果客户端这边设置很简单,只需要设置请求头[request addValue:@"text/event-stream" forHTTPHeaderField:@"Accept"];项目网络库用的AFN,经调研发现AFN不支持这个请求,最后选择了系统的NSURLSession......
  • 简单写一个eventbus
    前言闲暇之余,简单写一个eventbus。正文什么是eventbus?eventbus是一个开源的发布订阅模式的框架,用于简化程序间不同组件的通信。它允许不同组件间松耦合通信,组件之间不通过直接引用的方式,而是事件的方式进行消息传递。下面进行代码演示:首先是发布订阅,那么就应该有发布方法......
  • 【Linux系统编程】libevent库实现简易tcp服务器
    libevent库实现简易tcp服务器流程分析创建socket,设置端口复用,绑定四元组,开始监听。初始化event_base结构体。编写监听事件的回调函数和客户端读事件的回调函数。初始化tcp监听事件,并加入event_base中。开始event事件处理循环。释放所有事件占用资源。释放event_base占用......
  • 【Linux系统编程】libevent库事件驱动
    libevent库事件驱动libevent库使用创建并初始化event_base结构体。创建并初始化event结构体,并设置文件描述符、监听事件、回调函数、回调函数参数。将event添加到event_base中。开始事件处理循环,监听事件是否发生,并在满足条件时自动调用回调函数。事件处理完成后,释放event......
  • function ALV 获取OO ALV event ID
    SAPABAPALV(LVC)的一个自定义事件(F4帮助事件,回车ENTER按钮事件)的一个实例https://blog.csdn.net/zhongguomao/article/details/51775112 1.定义和注册事件接受器类*----------------------------------------------------------------------**CLASSLCL_EVENT_RECE......
  • html 元素的 onEvent 与 addEventListener
    对于html元素的onEvent,我们想要给其添加functionhandler(){},有时候会弄不清楚到底是添加<divonEvent="handler">还是添加<divonEvent="handler()">下面两段等价的代码说明了问题<inputtype="file"onchange="showFile(this)"><script>......
  • CountdownEvent
    CountdownEvent的注释为:表示在计数变为零时处于有信号状态的同步基元它是一个同步基元,它在收到一定次数的信号之后,将会解除对其等待线程的锁定。简的来说就是,事先设置需要有多少个通知,等待指定的通知数量全部到达后,Wait()才继续往下运行。代码示例如下:staticvoidMain(str......
  • 【Linux系统编程】libevent库介绍与安装
    libevent库介绍与安装libevent介绍libevent是一个异步事件处理软件函式库。libevent是一个提供异步事件通知的软件库。libevent提供了一组应用程序编程接口(API),libeventAPI提供的机制允许开发者为事件注册回调函数,例如文件描述符上的发生了特定事件或者等待特定事件超时,接收到......
  • Camera KMD ISP学习笔记(7)-CRM pipeline delay和V4L2 Event
    学习资料来源:https://deepinout.com/camx-kmd/camera-kmd-isp-subsystem-intro.html仅用于个人学习,侵联删 Realtimepipeline或实时Pipeline,在这个Pipeilne上的硬件设备有一些特点:实时,实时是指streamon后一直在出图,如下图的蓝色线(flash和actuator不会出图,但也是挂载到Realt......
  • addEventListener() 和 onclick 的区别
    前言:我们经常会看到有关addEventListener()和onclick的区别的标题,其实跟addEventListener()同级对比的应该是on+事件名称,不过在addEventListener()第一个参数为click的时候,addEventListener和onclick则值得作比较,下面就从三个角度来说说它们的区别。1.用法1.1addEven......