
Divided & Conquer
文章平均质量分 76
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Maximum Average Subtree
Given therootof a binary tree, find the maximum average value of any subtree of that tree.(A subtree of a tree is any node of that tree plus all its descendants. The average value of a tree is the sum of its values, divided by the number of nodes.)Ex..原创 2021-06-25 13:53:42 · 370 阅读 · 0 评论 -
Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return 6.思路: 主要看原创 2014-01-19 13:51:15 · 571 阅读 · 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 评论 -
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 评论 -
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 N-ary Tree
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]Output: 5思路:BFS level order/*// Definition for a Node.class Node { public int val...原创 2020-02-16 06:05:29 · 185 阅读 · 0 评论 -
Subtree of Another Tree
Given two non-empty binary treessandt, check whether treethas exactly the same structure and node values with a subtree ofs. A subtree ofsis a tree consists of a node insand all of this node...原创 2020-04-06 09:53:00 · 159 阅读 · 0 评论 -
Number of Good Leaf Nodes Pairs
Given therootof a binary tree and an integerdistance. A pair of two differentleafnodes of a binary tree is said to be good if the length ofthe shortest pathbetween them is less than or equal todistance.Returnthe number of good leaf node pairsin...原创 2020-07-26 12:59:02 · 355 阅读 · 0 评论 -
Tree的Traverse and divided & conquer
Tree的traverse,Preorder, Inorder, Postorder ,这些都是用stack来模拟考察的比较多。参考这里:PreOrder, InOrder, PostOrder 题型总结这里主要总结,divide and conquer 逻辑,往上返回result的情况;Lowest Common Ancestor of a Binary Search TreeLowest Common Ancestor of a Binary TreeClosest Binary S..原创 2020-07-07 08:44:01 · 448 阅读 · 1 评论 -
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 评论 -
All Possible Full Binary Trees
Afull binary treeis a binary tree where each node has exactly 0 or 2children.Return a list of all possible full binary trees withNnodes. Each element of the answer is the root node of one possible tree.Eachnodeof eachtree in the answermustha...原创 2020-06-08 11:15:12 · 160 阅读 · 0 评论 -
Binary Tree Longest Consecutive Sequence II
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree.Especially, this path can be either increasing or decreasing. For example, [1,2,3,4] and [4,3,2,1] are both considered valid, but the path [1,2,4,3] is not valid.原创 2020-06-06 13:17:33 · 265 阅读 · 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 评论 -
Smallest Subtree with all the Deepest Nodes
Given a binary tree rooted atroot, thedepthof each node is the shortest distance to the root.A node isdeepestif it has the largest depth possible amongany node in theentire tree.The subtree of a node is that node, plus the set of all descendants...原创 2020-05-25 14:59:49 · 244 阅读 · 0 评论 -
Split BST
Given a Binary Search Tree (BST) with root noderoot, and a target valueV, split the tree into two subtreeswhere one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the targ...原创 2020-05-21 12:44:06 · 249 阅读 · 0 评论 -
Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries asLandR, 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 result should return the new root of the trimmed binary se...原创 2020-05-13 12:40:37 · 149 阅读 · 0 评论 -
Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries asLandR, 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 result should return the new root of the trimmed binary se...原创 2020-05-13 12:40:05 · 226 阅读 · 0 评论 -
Delete Leaves With a Given Value
Given a binary treerootand an integertarget, delete all theleaf nodeswith valuetarget.Notethat once you delete a leaf node with valuetarget,if it's parent node becomes a leaf node and has t...原创 2020-04-29 12:15:25 · 194 阅读 · 0 评论 -
Second Minimum Node In a Binary Tree
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactlytwoorzerosub-node. If the node has two sub-nodes, then this node's va...原创 2020-03-30 11:39:52 · 221 阅读 · 0 评论 -
Range Sum of BST
Given therootnode of a binary search tree, return the sum of values of all nodes with value betweenLandR(inclusive).The binary search tree is guaranteed to have unique values.Example 1:...原创 2020-03-27 10:26:20 · 182 阅读 · 0 评论 -
Delete Nodes and Return Forest
Given therootof a binary tree, each node in the tree has a distinct value.After deletingall nodes with a value into_delete, we are left with a forest (adisjoint union of trees).Return the roo...原创 2020-03-15 02:57:04 · 350 阅读 · 0 评论 -
Binary Tree Upside Down
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the origin...原创 2014-12-09 14:02:17 · 884 阅读 · 0 评论 -
Binary Tree - Divide Conquer & Traverse 题型总结
place holder原创 2020-02-18 11:39:54 · 281 阅读 · 0 评论 -
Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.思路:recursion:左边为空的时候,只返回右边的+1,右...原创 2016-06-29 14:30:35 · 203 阅读 · 0 评论 -
House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour...原创 2016-10-04 08:56:55 · 297 阅读 · 0 评论 -
Merge k Sorted Lists
Mergeksorted linked lists and return it as one sorted list.Analyze and describe its complexity.ExampleExample 1: Input: [2->4->null,null,-1->null] Output: -1->2->4->nul...原创 2018-12-29 09:13:52 · 234 阅读 · 0 评论 -
Merge K Sorted Arrays
Givenksorted integer arrays, merge them into one sorted array.ExampleExample 1:Input: [ [1, 3, 5, 7], [2, 4, 6], [0, 8, 9, 10, 11] ]Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10...原创 2019-11-11 13:59:05 · 583 阅读 · 0 评论 -
Lowest Common Ancestor III
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.Ret...原创 2019-11-07 13:46:37 · 234 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Tree
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 ...原创 2016-08-02 13:05:45 · 327 阅读 · 0 评论 -
Flatten Binary Tree to Linked List
Flatten a binary tree to a fake "linked list" in pre-order traversal.Here we use therightpointer in TreeNode as thenextpointer in ListNode.ExampleExample 1:Input:{1,2,5,3,4,#,6}Output:{1...原创 2014-01-12 14:35:48 · 560 阅读 · 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 评论 -
Minimum Subtree
Given a binary tree, find the subtree with minimum sum. Return the root of the subtree.ExampleExample 1:Input:{1,-5,2,1,2,-4,-5}Output:1Explanation:The tree is look like this: 1 / ...原创 2019-11-06 14:32:14 · 321 阅读 · 1 评论