识别信号
- Do you know why inorder on [1,null,2,3] must delay visiting 2 until after processing 3?
- Can you explain how your traversal keeps track of where to return after finishing a left subtree?
- Do you consider every integer as a potential root when generating BSTs?
解题流程
- 1. 明确窗口/状态定义
- 2. 更新状态并维护约束
- 3. 用边界样例验证
常见失分点
- Appending a node value before fully traversing its left subtree, which turns the result into preorder-like output instead of inorder.
- Failing to combine all left and right subtree pairs for each root, leading to missing BSTs.
- Not memoizing the results of subproblems, leading to redundant calculations.
推荐题单梯度
二叉树的中序遍历
Binary Tree Inorder Traversal asks you to visit left subtree, node, then right subtree without losing position while mov…
不同的二叉搜索树 II
Generate all structurally unique BSTs with values 1 to n using backtracking and recursive tree construction techniques.
不同的二叉搜索树
Given n nodes, calculate the number of unique binary search trees (BSTs) that can be formed with values from 1 to n.
验证二叉搜索树
Validate Binary Search Tree problem checks if a binary tree satisfies BST properties using tree traversal and state trac…
恢复二叉搜索树
Recover a BST where two nodes are swapped by mistake using in-order traversal and careful state tracking to restore corr…
相同的树
Check whether two binary trees are identical by comparing structure and node values using DFS or BFS traversal strategie…
对称二叉树
Determine if a binary tree is symmetric by comparing left and right subtrees using DFS or BFS traversal techniques effic…
二叉树的层序遍历
Perform a level order traversal on a binary tree using BFS to return node values level by level.
二叉树的锯齿形层序遍历
Traverse a binary tree in zigzag level order, alternating directions at each depth using BFS and state tracking techniqu…
二叉树的最大深度
Find the maximum depth of a binary tree using traversal techniques to track the longest path from root to leaf.
二叉树的层序遍历 II
Return a bottom-up level order traversal of a binary tree, processing nodes left to right while tracking each level accu…
将有序数组转换为二叉搜索树
Pick the middle element as root at each step so the sorted array becomes a height-balanced BST with valid ordering.
平衡二叉树
Determine if a binary tree is height-balanced using tree traversal and state tracking techniques.
二叉树的最小深度
Find the minimum depth of a binary tree, which is the shortest path from the root node to the nearest leaf node.
路径总和
Determine if a binary tree has a root-to-leaf path where the sum of node values equals a given target sum, using DFS or …
路径总和 II
Find all root-to-leaf paths in a binary tree where the sum of node values equals a given target using DFS backtracking.
二叉树中的最大路径和
Calculate the maximum sum of any path in a binary tree by exploring all node sequences using DFS and dynamic programming…
求根节点到叶节点数字之和
Calculate the sum of all root-to-leaf numbers in a binary tree where each path represents a number.
二叉树的前序遍历
Perform a binary tree preorder traversal by visiting root nodes first, then left and right subtrees, tracking state iter…
二叉树的后序遍历
Solve Binary Tree Postorder Traversal using state tracking and binary-tree traversal techniques, focusing on Stack, Tree…
二叉搜索树迭代器
Implement an iterator for in-order traversal of a binary search tree (BST), maintaining traversal state with stack-based…
二叉树的右视图
The Binary Tree Right Side View problem asks you to return the visible nodes from the right side of a binary tree, trave…
完全二叉树的节点个数
Count Complete Tree Nodes efficiently by leveraging binary-tree traversal, exploiting completeness, and applying bit man…
翻转二叉树
Invert Binary Tree swaps every node's left and right children using tree traversal, with recursive DFS or iterative BFS …
二叉搜索树中第 K 小的元素
Find the kth smallest element in a BST by leveraging in-order traversal to efficiently track node order and count.
二叉搜索树的最近公共祖先
Find the lowest common ancestor (LCA) of two nodes in a binary search tree, using binary-tree traversal and state tracki…
二叉树的最近公共祖先
Identify the lowest common ancestor of two nodes in a binary tree using traversal and state tracking techniques efficien…
二叉树的所有路径
Find all root-to-leaf paths in a binary tree using DFS and backtracking, constructing strings for each complete path eff…
二叉树的序列化与反序列化
This problem asks to serialize and deserialize a binary tree, requiring an efficient approach to handle traversal and st…
验证二叉树的前序序列化
Determine if a given string correctly represents a binary tree preorder traversal using state tracking and slot counting…
打家劫舍 III
Maximize the loot by robbing non-adjacent houses in a binary tree structure using dynamic programming and DFS.
扁平化嵌套列表迭代器
Implement an iterator to flatten a nested list of integers, accounting for potential nesting levels.
左叶子之和
Compute the total of all left leaves in a binary tree using precise traversal and state tracking techniques for correctn…
建立四叉树
Construct a Quad-Tree from a binary matrix by recursively subdividing regions and tracking uniform states efficiently.
N 叉树的层序遍历
Perform a level order traversal on an n-ary tree, capturing nodes by depth using breadth-first search with careful state…
路径总和 III
Path Sum III challenges you to count paths in a binary tree that sum to a given target value with downward traversal.
序列化和反序列化二叉搜索树
Design an algorithm to serialize and deserialize a binary search tree with efficient traversal and state tracking.
删除二叉搜索树中的节点
Delete Node in a BST hinges on removing one target while preserving in-order ordering through carefully chosen subtree r…
二叉搜索树中的众数
Find Mode in Binary Search Tree asks to identify the most frequent element(s) in a BST using binary-tree traversal.
出现次数最多的子树元素和
Identify the most frequent subtree sum in a binary tree using DFS and hash table tracking for efficient state management…
找树左下角的值
Find the leftmost value in the last row of a binary tree using efficient traversal strategies.
在每个树行中找最大值
Find the largest value in each row of a binary tree using depth-first or breadth-first search techniques.
二叉搜索树的最小绝对差
Find the minimum absolute difference between values of any two nodes in a BST using tree traversal and careful state tra…
把二叉搜索树转换为累加树
Convert a BST into a Greater Tree by updating each node’s value with the sum of all greater values in the tree.
二叉树的直径
The Diameter of Binary Tree problem involves finding the longest path between any two nodes using tree traversal techniq…
四叉树交集
Compute the logical OR of two binary matrices represented as Quad-Trees using recursive tree traversal and state propaga…
N 叉树的最大深度
Find the maximum depth of an N-ary tree, leveraging tree traversal techniques and state tracking.
二叉树的坡度
Calculate the sum of the binary tree's node tilts using tree traversal and state tracking.
另一棵树的子树
Determine if one binary tree is an exact subtree of another by comparing structure and node values recursively.
N 叉树的前序遍历
Solve the N-ary Tree Preorder Traversal problem using depth-first search and stack-based traversal methods.
N 叉树的后序遍历
Postorder traversal of an N-ary tree can be efficiently solved using DFS and stack-based methods, tracking state across …
根据二叉树创建字符串
Given a binary tree, construct a string representation based on preorder traversal while adhering to specific formatting…
合并二叉树
Merge Two Binary Trees requires combining nodes by summing overlapping values while preserving non-null nodes from eithe…
在二叉树中增加一行
The problem requires adding a row of nodes to a binary tree at a specified depth.
二叉树的层平均值
Calculate the average value of nodes on each level of a binary tree using traversal and state tracking.
寻找重复的子树
Identify all duplicate subtrees in a binary tree by efficiently tracking structure and values with depth-first traversal…
两数之和 IV - 输入二叉搜索树
Determine if a binary search tree contains two nodes whose values sum to a target using efficient traversal and state tr…
最大二叉树
Construct a maximum binary tree by recursively selecting the largest element and dividing the array into left and right …
输出二叉树
Print Binary Tree requires arranging a binary tree into a visually structured matrix using precise traversal and positio…
二叉树最大宽度
Determine the maximum width of a binary tree by calculating the width of each level and considering the positions of the…
修剪二叉搜索树
Trim a Binary Search Tree by maintaining its structure while removing nodes outside a given range.
二叉树中第二小的节点
Find the second minimum node in a binary tree by traversing the tree and tracking state.
最长同值路径
Find the longest path in a binary tree where all nodes share the same value using depth-first search efficiently.
二叉搜索树中的搜索
Locate a target value in a binary search tree and return the subtree rooted at that node using efficient tree traversal …
二叉搜索树中的插入操作
Insert a value into a Binary Search Tree while maintaining the BST properties, focusing on tree traversal and state trac…
数据流中的第 K 大元素
Find the kth largest element in a dynamic stream using binary-tree traversal and efficient state tracking with a min-hea…
二叉搜索树节点最小距离
Find the minimum difference between values of two different nodes in a Binary Search Tree using tree traversal and state…
二叉树剪枝
Binary Tree Pruning removes subtrees not containing a 1, applying binary-tree traversal with state tracking.
二叉树中所有距离为 K 的结点
Find all nodes at distance K from a target node in a binary tree using various tree traversal techniques.
具有所有最深节点的最小子树
Find the smallest subtree that contains all the deepest nodes in a binary tree.
叶子相似的树
Determine if two binary trees have identical leaf sequences using efficient traversal and state tracking techniques.
所有可能的真二叉树
Generate all possible full binary trees with n nodes, focusing on dynamic programming and binary tree traversal.
递增顺序搜索树
Rearrange a binary search tree so all nodes follow increasing order with only right children using in-order traversal.
完全二叉树插入器
Implement a data structure to insert nodes into a complete binary tree while preserving its completeness efficiently.
二叉搜索树的范围和
Given a BST and a range, calculate the sum of all node values within that range.
翻转等价二叉树
Determine if two binary trees are flip equivalent by recursively swapping subtrees.
二叉树的完全性检验
Determine if a binary tree is complete by verifying its node structure and leftmost placement in the last level.
单值二叉树
Determine if a binary tree is uni-valued using traversal and state tracking techniques.
监控二叉树
Determine the minimum number of cameras required to monitor every node in a binary tree using efficient DFS and state tr…
翻转二叉树以匹配先序遍历
Determine the minimum set of nodes to flip in a binary tree so its pre-order traversal matches a given voyage sequence.
在二叉树中分配硬币
Minimize the number of moves needed to distribute coins in a binary tree so that each node has exactly one coin.
二叉树的垂序遍历
Perform a vertical order traversal of a binary tree, sorting nodes by their values within columns.
从叶结点开始的最小字符串
Determine the lexicographically smallest string from a leaf to root using binary-tree traversal and careful state tracki…
二叉树的堂兄弟节点
Determine if two nodes in a binary tree are cousins by leveraging binary-tree traversal and state tracking.
最大二叉树 II
The Maximum Binary Tree II problem requires building a binary tree by inserting a value into a maximum binary tree while…
前序遍历构造二叉搜索树
Construct a binary search tree directly from a preorder traversal array using stack-based state tracking efficiently.
从根到叶的二进制数之和
Calculate the sum of binary numbers from root to leaf paths in a binary tree.
节点与其祖先之间的最大差值
Find the maximum absolute difference between a node and its ancestor in a binary tree.
从先序遍历还原二叉树
Recover a binary tree from its preorder traversal string by tracking node depth and reconstructing child relationships e…
从二叉搜索树到更大和树
Convert a Binary Search Tree to a Greater Sum Tree by accumulating all larger node values using reverse in-order travers…
根到叶路径上的不足节点
Remove nodes in a binary tree whose all root-to-leaf paths sum to less than a given limit using DFS traversal and state …
二叉树寻路
The problem involves finding the path from the root to a node in a zigzag-labelled binary tree.
最深叶节点的最近公共祖先
Find the lowest common ancestor of the deepest leaves in a binary tree using efficient traversal and state tracking tech…
二叉树着色游戏
A two-player game where players color nodes in a binary tree, aiming to outmaneuver each other by choosing adjacent node…
最大层内元素和
Find the level of a binary tree where the sum of its nodes is maximal, with an emphasis on binary-tree traversal.
在受污染的二叉树中查找元素
Recover values in a contaminated binary tree and efficiently check for existence using traversal and state tracking tech…
层数最深叶子节点的和
Find the sum of the deepest leaves in a binary tree by using tree traversal and state tracking techniques.
两棵二叉搜索树中的所有元素
Merge elements from two binary search trees and return them sorted in ascending order.
祖父节点值为偶数的节点和
This problem involves calculating the sum of nodes with an even-valued grandparent in a binary tree.
删除给定值的叶子节点
In this problem, you need to delete all leaf nodes in a binary tree with a given target value, performing continuous del…
分裂二叉树的最大乘积
Maximize the product of sums of two subtrees formed by splitting a binary tree.
二叉树中的最长交错路径
Find the longest ZigZag path in a binary tree using depth-first search and dynamic programming for precise node state tr…
二叉搜索子树的最大键值和
Find the maximum sum of values from any Binary Search Tree (BST) subtree in a binary tree.
通知所有员工所需的时间
Calculate the time needed for the head of a company to inform all employees using tree traversal techniques.
找出克隆二叉树中的相同节点
Find the corresponding node in a cloned binary tree using binary-tree traversal and state tracking.
将二叉搜索树变平衡
This problem requires balancing a binary search tree using in-order traversal and state tracking techniques.
收集树上所有苹果的最少时间
Minimize the time spent to collect all apples in a tree, considering traversal and state tracking with binary tree techn…
统计二叉树中好节点的数目
Count Good Nodes in Binary Tree identifies nodes exceeding all previous values along their path using DFS traversal tech…
二叉树中的伪回文路径
Count all root-to-leaf paths in a binary tree that can be rearranged to form a palindrome using bitwise state tracking.
树节点的第 K 个祖先
Find the kth ancestor of any node in a tree using efficient binary-tree traversal and dynamic state tracking methods.
子树中标签相同的节点数
Compute the number of nodes in each subtree sharing the same label using DFS with hash table aggregation efficiently.
好叶子节点对的数量
Find the number of good leaf node pairs in a binary tree where the shortest path between them is less than or equal to a…
将子数组重新排序得到同一个二叉搜索树的方案数
Determine the number of ways to reorder an array to get the same binary search tree (BST) from its insertion order.
王位继承顺序
Throne Inheritance requires modeling a dynamic family tree with births and deaths to determine the kingdom's inheritance…
奇偶树
Determine if a binary tree meets Even-Odd rules by checking value parity and strict ordering at each level efficiently.
统计子树中城市之间最大距离
This problem asks you to count subtrees in a tree structure where the maximum distance between any two cities matches sp…
重构一棵树的方案数
Determine how many distinct rooted trees can be reconstructed from given node pairs using careful traversal and state tr…
互质树
Determine the closest coprime ancestor for each node in a tree using efficient traversal and state tracking of node valu…
合并多棵二叉搜索树
This problem asks you to merge multiple BSTs into a single valid BST by performing a series of operations.
每棵子树内缺失的最小基因值
Determine the smallest missing genetic value in each subtree using binary-tree traversal and precise state tracking effi…
统计最高分的节点数目
Find the number of nodes with the highest score in a binary tree, based on subtree sizes and node removal.
从二叉树一个节点到另一个节点每一步的方向
Find the shortest path between two nodes in a binary tree and output the directions as a string of 'L', 'R', and 'U'.
判断根结点是否等于子结点之和
Check if a binary tree root value equals the sum of its immediate left and right child nodes efficiently.
统计值等于子树平均值的节点数
Given a binary tree, count nodes where the value equals the average of values in its subtree.
从树中删除边的最小分数
Compute the minimum score after removing two edges in a tree using DFS and XOR-based component tracking efficiently.
计算布尔二叉树的值
Determine the boolean outcome of a full binary tree by evaluating leaf and internal nodes using depth-first traversal.
感染二叉树需要的总时间
The problem asks to calculate the number of minutes for an infection to spread across all nodes in a binary tree startin…
反转二叉树的奇数层
Reverse the node values at each odd level in a perfect binary tree, preserving the even levels.
创建价值相同的连通块
Maximize the number of components in a tree with equal sums by carefully deleting edges using divisor-based logic.
移除子树后的二叉树高度
Compute the height of a binary tree efficiently after removing subtrees, using traversal and precomputed node state trac…
逐层排序二叉树所需的最少操作数目
Determine the minimum number of swaps to sort each level of a binary tree using level-wise traversal efficiently.
二叉搜索树最近节点查询
Solve the problem of finding closest nodes in a Binary Search Tree for multiple queries, efficiently handling each query…
查询树中环的长度
Solve the problem of determining cycle lengths in a binary tree with added edges through queries.
最大价值和与最小价值和的差值
Compute the maximum difference between any path price sum in a tree using binary-tree traversal and state tracking effic…
二叉树中的第 K 大层和
Find the kth largest level sum in a binary tree using level-order traversal and sorting.
二叉树的堂兄弟节点 II
Replace each node in a binary tree with the sum of all its cousins by carefully tracking depth and parent relationships.
使二叉树所有路径值相等的最小代价
Minimize the cost increments required to equalize path costs in a binary tree from root to leaves.
树中可以形成回文的路径数
This problem asks you to count all node pairs in a tree whose path characters can be rearranged into a palindrome using …
边权重均等查询
Find the minimum number of operations to equalize edge weights in a tree between given pairs of nodes.
统计树中的合法路径数目
Count Valid Paths in a Tree involves finding paths with exactly one prime number in a tree of n nodes.
可以被 K 整除连通块的最大数目
Determine the maximum number of connected components in a tree where each component sum is divisible by k using DFS.
收集所有金币可获得的最大积分
Find the maximum points after collecting coins from all nodes of a tree using binary-tree traversal and state tracking.
在树上执行操作以后得到的最大分数
Solve Maximum Score After Applying Operations on a Tree by turning healthy-path constraints into subtree DP and forced-v…
树中每个节点放置的金币数目
Determine the exact number of coins to place on each tree node using subtree cost products and DFS tracking.
在带权树网络中统计可连接服务器对数目
This problem involves counting pairs of connectable servers in a weighted tree network using binary-tree traversal and D…
最大节点价值之和
Solve Find the Maximum Sum of Node Values by tracking XOR gain parity, not by simulating edge operations across the tree…
统计好节点的数目
Determine how many nodes in a binary tree have all child subtrees of equal size using DFS traversal efficiently.
第 K 大的完美二叉子树的大小
Find the size of the kth largest perfect subtree in a binary tree using tree traversal and state tracking.
移除边之后的权重最大和
Maximize the sum of edge weights in a tree after removals, using dynamic programming and tree traversal techniques.
连接两棵树后最大目标节点数目 I
Maximize the number of target nodes after connecting two trees using binary tree traversal and state tracking.
连接两棵树后最大目标节点数目 II
Maximize the number of target nodes after connecting two trees by analyzing their structure and target relationships.
带权树中的最短路径
Solve the Shortest Path in a Weighted Tree using binary-tree traversal and efficient state tracking for queries.
子树反转和
This problem involves calculating the maximum possible subtree inversion sum with dynamic programming and binary-tree tr…
包含要求路径的最小带权子图 II
Solve the Minimum Weighted Subgraph With the Required Paths II problem by leveraging binary-tree traversal and efficient…
给边赋权值的方案数 I
Calculate the number of valid edge weight assignments in a tree using DFS and binary-tree traversal with careful state t…
给边赋权值的方案数 II
This problem involves assigning edge weights in a tree and calculating the cost of paths based on these weights.
折扣价交易股票的最大利润
Solve the Maximum Profit from Trading Stocks with Discounts problem using binary-tree traversal and dynamic state tracki…
最大好子树分数
Find the maximum sum of values in a tree subtree without repeating any digit across selected nodes using DFS and bitmask…
树中找到带权中位节点
Given a weighted tree and queries, find the weighted median node for each path between two nodes using binary-tree trave…
第 K 小的路径异或和
This problem involves finding the kth smallest distinct XOR sum for nodes in a subtree of a tree structure using binary-…
使叶子路径成本相等的最小增量
Find the minimum number of increments needed to equalize leaf path scores in a tree with different node costs.