
Tree
文章平均质量分 68
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Smallest Common Region
You are given some lists ofregionswhere the first region of each list includes all other regions in that list.Naturally, if a regionxcontains another regionythenxis bigger thany. Also, by definition, a regionxcontains itself.Given two region...原创 2022-05-15 06:48:44 · 141 阅读 · 0 评论 -
Reaching Points
Given four integers sx, sy, tx, and ty, return true if it is possible to convert the point (sx, sy) to the point (tx, ty) through some operations, or false otherwise.The allowed operation on some point (x, y) is to convert it to either (x, x + y) or (x +原创 2022-03-14 13:39:48 · 1876 阅读 · 0 评论 -
Verify Preorder Serialization of a Binary Tree
One way to serialize a binary tree is to usepreorder traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as'#'.For example, the above binary tree can be serialized to t..原创 2022-03-09 14:55:04 · 149 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Tree III
Given two nodes of abinary treepandq, returntheirlowest common ancestor (LCA).Each node will have a reference to its parent node. The definition forNodeis below:class Node { public int val; public Node left; public Node right; ...原创 2021-06-29 14:19:50 · 387 阅读 · 0 评论 -
Minimum Distance Between BST Nodes
Given the root of a Binary Search Tree (BST), return the minimum difference between the values of any two different nodes in the tree.Example 1:Input: root = [4,2,6,1,3]Output: 1Example 2:Input: root = [1,0,48,null,null,12,49]Output: 1Constraints:原创 2021-06-22 12:59:00 · 106 阅读 · 0 评论 -
Balance a Binary Search Tree
Given a binary search tree, return abalancedbinary search tree with the same node values.A binary search tree isbalancedif and only ifthe depth of the two subtrees ofeverynode never differ by more than 1.If there is more than one answer, return ...原创 2020-09-07 05:34:17 · 293 阅读 · 0 评论 -
Inorder Successor in BST II
Given anodein a binary search tree, findthe in-order successor of that node in the BST.If that node has no in-order successor, returnnull.The successor of anodeis the node with the smallest key greater thannode.val.You will have direct access ...原创 2020-07-06 08:43:03 · 376 阅读 · 0 评论 -
Distribute Coins in Binary Tree
Given therootof a binary tree withNnodes, eachnodein the tree hasnode.valcoins, and there areNcoins total.In one move, we may choose two adjacent nodes and move one coin from one node to another. (The move may be from parent to child, or from ...原创 2020-06-12 02:15:10 · 188 阅读 · 0 评论 -
Maximum Binary Tree
Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:The root is the maximum number in the array. The left subtree is the maximum tree constructed f...原创 2020-02-21 11:29:56 · 125 阅读 · 0 评论 -
Closest Leaf in a Binary Tree
Input:root = [1,2,3,4,null,null,null,5,null,6], k = 2Diagram of binary tree: 1 / \ 2 3 / 4 / 5 / 6Output: 3Expl...原创 2020-02-20 16:00:11 · 156 阅读 · 0 评论 -
Binary Tree - Lowest Common Ancestor 题型总结
Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedi...原创 2020-02-20 14:52:31 · 199 阅读 · 0 评论 -
Lowest Common Ancestor of Deepest Leaves
Given a rooted binary tree, return the lowest common ancestor of its deepest leaves.Recall that:The node of a binary tree is aleafif and only if it has no children Thedepthof the root of the ...原创 2020-02-20 14:47:47 · 365 阅读 · 0 评论 -
Binary Tree - Path Sum总结
Path Sum IGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Note:A leaf is a node with no childre...原创 2020-02-19 14:09:29 · 313 阅读 · 0 评论 -
Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals.Values in the traversalspreandpostare distinctpositive integers.Example 1:Input: pre = [1,2,4,5,3,6,7], p...原创 2020-02-19 11:43:10 · 226 阅读 · 0 评论 -
Construct Binary Search Tree from Preorder Traversal
Return the root node of a binarysearchtree that matches the givenpreordertraversal.(Recall that a binary search treeis a binary tree where for everynode, any descendant ofnode.lefthas a valu...原创 2020-02-18 14:25:05 · 323 阅读 · 0 评论 -
Inorder Predecessor in BST
Given a binary search tree and a node in it, find the in-order predecessor of that node in the BST.ExampleExample1Input: root = {2,1,3}, p = 1Output: nullExample2Input: root = {2,1}, p =...原创 2019-11-19 12:57:13 · 470 阅读 · 0 评论 -
Lowest Common Ancestor II
Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes.The lowest common ancestor is the node with largest depth which is the ancestor of both nodes.T...原创 2019-11-07 12:37:25 · 180 阅读 · 0 评论 -
Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum =...原创 2014-01-16 13:46:44 · 515 阅读 · 0 评论 -
Verify Preorder Sequence in Binary Search Tree
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.You may assume each number in the sequence is unique.Follow up:Could you do it usi原创 2016-10-03 13:52:04 · 300 阅读 · 0 评论 -
Path Sum III
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it ...原创 2016-10-28 07:04:48 · 561 阅读 · 0 评论 -
Delete Node in a BST
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.Basically, the deletion can be divided ...原创 2016-11-13 06:55:54 · 1321 阅读 · 0 评论 -
Find Leaves of Binary Tree
Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.Example:Given binary tree 1 / \ ...原创 2016-10-01 12:36:10 · 297 阅读 · 0 评论 -
Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.思路1: traverse的方法,就是访问所有的点,然后记录一些信息,从而最...原创 2016-06-17 14:35:05 · 273 阅读 · 0 评论 -
Validate Binary Search Tree
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. The ...原创 2014-01-17 13:18:43 · 647 阅读 · 0 评论 -
Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]思...原创 2016-07-11 14:05:15 · 238 阅读 · 0 评论 -
Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next() ...原创 2015-01-11 13:03:24 · 617 阅读 · 0 评论 -
Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to...原创 2016-08-18 14:01:25 · 445 阅读 · 0 评论 -
Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.Note: If the given node has no in-order successor in the tree, returnnull.Example 1:Input: root...原创 2016-08-04 09:50:50 · 563 阅读 · 0 评论 -
Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.Example 1:Input: 3 / \ 9 20 / \ 15 7Output: [3, 14.5, 11]Explanati...原创 2018-12-30 13:17:00 · 153 阅读 · 0 评论 -
Merge Two Binary Trees
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree...原创 2018-12-25 13:14:00 · 136 阅读 · 0 评论 -
Sum of Left Leaves
Find the sum of all left leaves in a given binary tree.Example: 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return ...原创 2016-09-27 14:25:01 · 782 阅读 · 0 评论 -
Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the re...原创 2019-01-02 04:21:51 · 131 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betwee...原创 2016-08-02 13:07:39 · 268 阅读 · 0 评论 -
Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it.Note:A subtree must include all of its descendan原创 2016-08-03 07:27:43 · 633 阅读 · 0 评论 -
Unique Binary Search Trees II
Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3原创 2014-12-15 15:25:22 · 494 阅读 · 0 评论 -
Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solution is trivial...原创 2014-01-17 12:28:20 · 1038 阅读 · 0 评论 -
Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a原创 2014-01-27 16:08:47 · 552 阅读 · 0 评论 -
Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Recursive solution is trivi...原创 2014-01-10 14:42:18 · 828 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:找中间点,就是root,然后两边分别构造左子树和右子树。/** * Definition for a binary tree node. * public class TreeNode ...原创 2016-07-18 08:24:47 · 339 阅读 · 0 评论 -
Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as:a binary tree in which the depth of the two subtrees ofeverynode never diff...原创 2014-01-12 16:17:08 · 475 阅读 · 0 评论