返回介绍

solution / 1400-1499 / 1430.Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree / README_EN

发布于 2024-06-17 01:03:19 字数 5533 浏览 0 评论 0 收藏 0

1430. Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree

中文文档

Description

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,0,1]
Output: false 
Explanation: The path 0 -> 0 -> 1 does not exist, therefore it is not even a sequence.

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].

Solutions

Solution 1

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, val=0, left=None, right=None):
#     self.val = val
#     self.left = left
#     self.right = right
class Solution:
  def isValidSequence(self, root: TreeNode, arr: List[int]) -> bool:
    def dfs(root, u):
      if root is None or root.val != arr[u]:
        return False
      if u == len(arr) - 1:
        return root.left is None and root.right is None
      return dfs(root.left, u + 1) or dfs(root.right, u + 1)

    return dfs(root, 0)
/**
 * 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 {
  private int[] arr;

  public boolean isValidSequence(TreeNode root, int[] arr) {
    this.arr = arr;
    return dfs(root, 0);
  }

  private boolean dfs(TreeNode root, int u) {
    if (root == null || root.val != arr[u]) {
      return false;
    }
    if (u == arr.length - 1) {
      return root.left == null && root.right == null;
    }
    return dfs(root.left, u + 1) || dfs(root.right, u + 1);
  }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *   TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *   TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  bool isValidSequence(TreeNode* root, vector<int>& arr) {
    function<bool(TreeNode*, int)> dfs = [&](TreeNode* root, int u) -> bool {
      if (!root || root->val != arr[u]) return false;
      if (u == arr.size() - 1) return !root->left && !root->right;
      return dfs(root->left, u + 1) || dfs(root->right, u + 1);
    };
    return dfs(root, 0);
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func isValidSequence(root *TreeNode, arr []int) bool {
  var dfs func(root *TreeNode, u int) bool
  dfs = func(root *TreeNode, u int) bool {
    if root == nil || root.Val != arr[u] {
      return false
    }
    if u == len(arr)-1 {
      return root.Left == nil && root.Right == nil
    }
    return dfs(root.Left, u+1) || dfs(root.Right, u+1)
  }
  return dfs(root, 0)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文