https://leetcode.cn/problems/number-of-ways-to-reconstruct-a-tree 这道题曾经在周赛难度分top1呆了几年,如今已经不在难度分top5了。破局的关键是父节点的邻居数肯定不少于子节点,而根节点的邻居数一定是n-1。所以应该先找到根节点,然后逐层向下建树,子节点的邻居一定是父节点的子集,否则就是非法;如果子...
public void flatten(TreeNode root) { if(root==null) return; TreeNode curr = root; while(curr!=null){ //保存右子树, 后面用 TreeNode oldRight = curr.right; //左子树不为空, 执行一系列改变指向的操作 if(curr.left!=null){ //找到左子树的最右节点 TreeNode rightMost = curr.left; while(...
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow...
Check the consistency of the numbers (Trie Data Structure Question) Constructing the Array Range Minimum Query PPATH - Prime Path Problem Leetcode Problem Solution Count Number of Nodes in a Complete Binary Tree (Leetcode Problem Solution)
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. ...
what is the value in the case of HashSet? Well, in the case of HashSet a dummy object is used as a value and key objects are the actual element on Set point of view. Since HashMap doesn't allow duplicate keys it also follows the contract of set data structure to not allow ...
LeetCode Top 100 Liked Questions 102. Binary Tree Level Order Traversal (Java版; Medium) 题目描述 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:
LeetCode Top 100 Liked Questions 98. Validate Binary Search Tree (Java版; Medium) 题目描述 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key...