[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相关推荐

  1. 【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 ...

  2. 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, ...

  3. C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...

  4. [LeetCode] Binary Tree Level Order Traversal 二叉树层次遍历(DFS | BFS)

    目录: 1.Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 2.Binary Tree Level Order Traversal II - 二叉树层次 ...

  5. leetcode @python 124. Binary Tree Maximum Path Sum

    题目链接 https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题目原文 Given a binary tree, find the ...

  6. 如何检查字符串是否包含特定的单词? [英]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, ...

  7. leetcode 606. Construct String from Binary Tree | 606. 根据二叉树创建字符串

    题目 https://leetcode-cn.com/problems/construct-string-from-binary-tree/ 题解 当右子树非空时,不管有没有左子树,一定要用()将左子 ...

  8. LeetCode 606. Construct String from Binary Tree

    题目: You need to construct a string consists of parenthesis and integers from a binary tree with the ...

  9. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

最新文章

  1. VO 1 先弄明白在干什么
  2. 【译】10 years Blockchain. The Race is on: Blockchain vs. Tangle vs. Hashgraph
  3. 【H.264/AVC视频编解码技术】第四章【SPS序列参数集】
  4. 使用JQuery结合HIghcharts实现从后台获取JSON实时刷新图表
  5. SCMagazine: SIEM走出阴影,迈向光明
  6. 四款主流测试工具的测试流程
  7. clickinrefresh.php,IDCZE_v3.0 IDCZE商业导航系统全新仿照114la网址导航 - 下载 - 搜珍网...
  8. 【渝粤教育】电大中专消费者心理学作业 题库
  9. URL地址相对路径转绝对路径
  10. LintCode—两数组的交(547)
  11. java google翻译api接口_java 免费调用google 谷歌翻译api
  12. Java夜未眠(蔡学镛)
  13. 微信HOOK 退出群聊
  14. stanford coreNLP简单使用
  15. Win7和Win10操作系统优劣对比,看完你就懂了!
  16. 遇到了 “遇到以零作除数错误” 的问题
  17. 这一年,你遇见了谁?
  18. Python算法题笔记
  19. NXP JN5169 使用看门狗定时器
  20. socket 发送 TCP和UDP方式

热门文章

  1. win7扫描仪在计算机,怎么在win7我的电脑里显示hp1005扫描仪图标
  2. 地表温度LST计算教程
  3. 从零开始创建react+Django项目
  4. t6修改服务器ip,用友t6服务器更换ip地址
  5. 全国银行简码数据(JSON)
  6. Diskeeper 2007 Professional Premier Edition 11.0 Build 686.1t
  7. 图像处理实验,中值滤波处理椒盐噪声
  8. “大玩家”登场,暴风TV的AI版图再下一城
  9. HashMap源码总结(持续更新中)
  10. 毕业设计之基于Vue的数据可视化平台