LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree
[Med] LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree
链接: https://leetcode.com/problems/check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree/
题目描述:
Given a binary tree where each path going from the root to any leaf form a valid sequence, check if a given string is a valid sequence in such binary tree.
We get the given string from the concatenation of an array of integers arr and the concatenation of all values of the nodes along a path results in a sequence in the given binary tree.
Example 1:
Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,0,1]
Output: true
Explanation:
The path 0 -> 1 -> 0 -> 1 is a valid sequence (green color in the figure).
Other valid sequences are:
0 -> 1 -> 1 -> 0
0 -> 0 -> 0
Example 2:
Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,1]
Output: false
Explanation: The path 0 -> 1 -> 1 is a sequence, but it is not a valid sequence.
Constraints:
Example 3:
Input: root = [0,1,0,0,1,0,null,null,1,0,0], arr = [0,1,1]
Output: false
Explanation: The path 0 -> 1 -> 1 is a sequence, but it is not a valid sequence.
Constraints:
- 1 <= arr.length <= 5000
- 0 <= arr[i] <= 9
- Each node’s value is between [0 - 9].
Tag: Pre-Sum
解题思路
这道题目的意思还是很简单的,就是让我们找到一条从root贯穿到leaf的path
但是比较令人难受的是这道题边界条件还是挺多的。比如说,从root到leaf这一个条件就比较麻烦。如果整个树只有根节点自己,那么如果数组也有且只有一位并且可以跟根节点match的话。那么就算是true。但是如果根节点左边还有一个元素,那么根节点就不算是leaf了。
比如说数组是[1],树的结构是
1
/
3
这样就不能算是从根节点match到leaf了,因为1并不是左右都为空的节点。所以这样的就会要返回false。
所以就此推测,我们判断某一个节点是不是leaf node的依据就是这个节点的左右两个子节点是不是空的。如果是的话,说明我们到达了leaf node。此时我们看当前节点的值是不是跟数组当中idx的位置上的节点的值一致。我们还需要判断此时数组节点是不是同样也是最后一个了。如果两个条件都是true的话,那么我们就返回true。如果某一个节点为空或者数组此时已经遍历完了的话,说明没有兜住上面这个条件。所以此时返回false。因为我们只需要有一条path完成就算是完成了,所以每一个节点的左右subtree中只要有宜宾完成了的话就是true.
时间复杂度和空间复杂度都是O(N)
解法一:
class Solution {public boolean isValidSequence(TreeNode root, int[] arr) {return helper(root, arr, 0);}public boolean helper(TreeNode root, int[] arr, int idx){if(root == null || idx == arr.length || arr[idx] != root.val){return false;}if(root != null && root.left==null && root.right == null && idx==arr.length-1 && arr[idx]==root.val){return true;}boolean left = helper(root.left, arr, idx+1) ;boolean right = helper(root.right, arr, idx+1);return left || right;}
}/*** 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;* }* }*/
LeetCode 1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree相关推荐
- 【Leetcode】1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree
题目地址: https://leetcode.com/problems/check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-i ...
- leetcode 1662. Check If Two String Arrays are Equivalent(python)
描述 Given two string arrays word1 and word2, return true if the two arrays represent the same string, ...
- C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)
目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...
- leetcode @python 124. Binary Tree Maximum Path Sum
题目链接 https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题目原文 Given a binary tree, find the ...
- 如何检查字符串是否包含特定的单词? [英]How do I check if a string contains a specific word?
Consider: 考虑: $a = 'How are you?';if ($a contains 'are')echo 'true'; Suppose I have the code above, ...
- leetcode 606. Construct String from Binary Tree | 606. 根据二叉树创建字符串
题目 https://leetcode-cn.com/problems/construct-string-from-binary-tree/ 题解 当右子树非空时,不管有没有左子树,一定要用()将左子 ...
- LeetCode 606. Construct String from Binary Tree
题目: You need to construct a string consists of parenthesis and integers from a binary tree with the ...
- LeetCode刷题记录14——257. Binary Tree Paths(easy)
LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...
最新文章
- VO 1 先弄明白在干什么
- 【译】10 years Blockchain. The Race is on: Blockchain vs. Tangle vs. Hashgraph
- 【H.264/AVC视频编解码技术】第四章【SPS序列参数集】
- 使用JQuery结合HIghcharts实现从后台获取JSON实时刷新图表
- SCMagazine: SIEM走出阴影,迈向光明
- 四款主流测试工具的测试流程
- clickinrefresh.php,IDCZE_v3.0 IDCZE商业导航系统全新仿照114la网址导航 - 下载 - 搜珍网...
- 【渝粤教育】电大中专消费者心理学作业 题库
- URL地址相对路径转绝对路径
- LintCode—两数组的交(547)
- java google翻译api接口_java 免费调用google 谷歌翻译api
- Java夜未眠(蔡学镛)
- 微信HOOK 退出群聊
- stanford coreNLP简单使用
- Win7和Win10操作系统优劣对比,看完你就懂了!
- 遇到了 “遇到以零作除数错误” 的问题
- 这一年,你遇见了谁?
- Python算法题笔记
- NXP JN5169 使用看门狗定时器
- socket 发送 TCP和UDP方式
热门文章
- win7扫描仪在计算机,怎么在win7我的电脑里显示hp1005扫描仪图标
- 地表温度LST计算教程
- 从零开始创建react+Django项目
- t6修改服务器ip,用友t6服务器更换ip地址
- 全国银行简码数据(JSON)
- Diskeeper 2007 Professional Premier Edition 11.0 Build 686.1t
- 图像处理实验,中值滤波处理椒盐噪声
- “大玩家”登场,暴风TV的AI版图再下一城
- HashMap源码总结(持续更新中)
- 毕业设计之基于Vue的数据可视化平台