原文链接: https://leetcode-cn.com/problems/pseudo-palindromic-paths-in-a-binary-tree 英文原文Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node valu…
标签详情
深度优先搜索 分类
原文链接: https://leetcode-cn.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero 英文原文There are n cities numbered from 0 to n - 1 and n - 1 roads such that there is only one way to travel between two different cities (this network form a …
原文链接: https://leetcode-cn.com/problems/number-of-nodes-in-the-sub-tree-with-the-same-label 英文原文Given a tree (i.e. a connected, undirected graph that has no cycles) consisting of n nodes numbered from 0 to n - 1 and exactly n - 1 edges. The root of th…
原文链接: https://leetcode-cn.com/problems/number-of-good-leaf-nodes-pairs 英文原文Given the root of a binary tree and an integer distance. A pair of two different leaf nodes of a binary tree is said to be good if the length of the shortest path between them…
原文链接: https://leetcode-cn.com/problems/detect-cycles-in-2d-grid 英文原文Given a 2D array of characters grid of size m x n, you need to find if there exists any cycle consisting of the same value in grid. A cycle is a path of length 4 or more in the grid …
原文链接: https://leetcode-cn.com/problems/minimum-number-of-days-to-disconnect-island 英文原文Given a 2D grid consisting of 1s (land) and 0s (water). An island is a maximal 4-directionally (horizontal or vertical) connected group of 1s. The grid is said to …
原文链接: https://leetcode-cn.com/problems/throne-inheritance 英文原文A kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while, someone in the family dies or a child is born. The kingdom has a well-defined order of inhe…
原文链接: https://leetcode-cn.com/problems/path-with-minimum-effort 英文原文You are a hiker preparing for an upcoming hike. You are given heights, a 2D array of size rows x columns, where heights[row][col] represents the height of cell (row, col). You are si…
原文链接: https://leetcode-cn.com/problems/merge-bsts-to-create-single-bst 英文原文You are given n BST (binary search tree) root nodes for n separate BSTs stored in an array trees (0-indexed). Each BST in trees has at most 3 nodes, and no two roots have the …
原文链接: https://leetcode-cn.com/problems/last-day-where-you-can-still-cross 英文原文There is a 1-based binary matrix where 0 represents land and 1 represents water. You are given integers row and col representing the number of rows and columns in the matri…
原文链接: https://leetcode-cn.com/problems/find-if-path-exists-in-graph 英文原文There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, whe…
原文链接: https://leetcode-cn.com/problems/smallest-missing-genetic-value-in-each-subtree 英文原文There is a family tree rooted at 0 consisting of n nodes numbered 0 to n - 1. You are given a 0-indexed integer array parents, where parents[i] is the parent fo…
原文链接: https://leetcode-cn.com/problems/count-nodes-with-the-highest-score 英文原文There is a binary tree rooted at 0 consisting of n nodes. The nodes are labeled from 0 to n - 1. You are given a 0-indexed integer array parents representing the tree, wher…
原文链接: https://leetcode-cn.com/problems/find-all-people-with-secret 英文原文You are given an integer n indicating there are n people numbered from 0 to n - 1. You are also given a 0-indexed 2D integer array meetings where meetings[i] = [xi, yi, timei] ind…
原文链接: https://leetcode-cn.com/problems/route-between-nodes-lcci 英文原文Given a directed graph, design an algorithm to find out whether there is a route between two nodes. Example1: Input: n = 3, graph = [[0, 1], [0, 2], [1, 2], [1, 2]], start = 0, targe…
原文链接: https://leetcode-cn.com/problems/check-balance-lcci 英文原文Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node…
原文链接: https://leetcode-cn.com/problems/legal-binary-search-tree-lcci 英文原文Implement a function to check if a binary tree is a binary search tree. Example 1: Input: 2 / \ 1 3 Output: true Example 2: Input: 5 / \ 1 4 / \ 3 6 Output: false Explanation: I…
原文链接: https://leetcode-cn.com/problems/successor-lcci 英文原文Write an algorithm to find the "next" node (i.e., in-order successor) of a given node in a binary search tree. Return null if there's no "next" node for the given node. Example 1: Input: root …
原文链接: https://leetcode-cn.com/problems/first-common-ancestor-lcci 英文原文Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessaril…
原文链接: https://leetcode-cn.com/problems/color-fill-lcci 英文原文Implement the "paint fill" function that one might see on many image editing programs. That is, given a screen (represented by a two-dimensional array of colors), a point, and a new color, fi…
原文链接: https://leetcode-cn.com/problems/check-subtree-lcci 英文原文T1 and T2 are two very large binary trees. Create an algorithm to determine if T2 is a subtree of T1. A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of n…
原文链接: https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof 中文题目输入两棵二叉树A和B,判断B是不是A的子结构。(约定空树不是任意一个树的子结构) B是A的子结构, 即 A中有出现和B相同的结构和节点值。 例如: 给定的树 A: 3 / \ 4 5 / \ 1 2 给定的树 B: 4 / 1 返回 true,因为 B 与 A 的一个子树拥有相同的结构和节点值。 示例 1: 输入:A = [1,2,3], B = [3,1] 输出:…
原文链接: https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof 中文题目请完成一个函数,输入一个二叉树,该函数输出它的镜像。 例如输入: 4 / \ 2 7 / \ / \ 1 3 6 9 镜像输出: 4 / \ 7 2 / \ / \ 9 6 3 1 示例 1: 输入:root = [4,2,7,1,3,6,9] 输出:[4,7,2,9,6,3,1] 限制: 0 <= 节点个数 <= 1000 注意:本题与主站 226 …
原文链接: https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof 中文题目请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / \ 2 2 \ \ 3 3 示例 1: 输入:root = [1…
原文链接: https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof 中文题目输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表。要求不能创建任何新的节点,只能调整树中节点指针的指向。 为了让您更好地理解问题,以下面的二叉搜索树为例: 我们希望将这个二叉搜索树转化为双向循环链表。链表中的每个节点都有一个前驱和后继指针。对于双向循环链表,第一个节点的前驱是最后一个节点,最后一个节…
原文链接: https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof 中文题目请实现两个函数,分别用来序列化和反序列化二叉树。 你需要设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。 提示:输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这…
原文链接: https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof 中文题目给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。 叶子节点 是指没有子节点的节点。 示例 1: 输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 输出…
原文链接: https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof 中文题目输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最大深度 3 。 提示: 节点总数 <= 10000 注意:本题与主站 104 题相同:https://leet…
原文链接: https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof 中文题目给定一棵二叉搜索树,请找出其中第k大的节点。 示例 1: 输入: root = [3,1,4,null,2], k = 1 3 / \ 1 4 \ 2 输出: 4 示例 2: 输入: root = [5,3,6,2,4,null,null,1], k = 3 5 / \ 3 6 / \ 2 4 / 1 输出: 4 限制: 1 …
原文链接: https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof 中文题目输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。 示例 1: 给定二叉树 [3,9,20,null,null,15,7] 3 / \ 9 20 / \ 15 7 返回 true 。 示例 2: 给定二叉树 [1,2,2,3,3,null,null,4,4] 1 / \ 2 2 / …