
traverse
文章平均质量分 74
flyatcmu
这个作者很懒,什么都没留下…
展开
-
Complete Binary Tree Inserter
Acomplete binary treeis a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.Design an algorithm to insert a new node to a complete binary tree keeping it complete after the inser..原创 2022-04-05 14:12:00 · 101 阅读 · 0 评论 -
Step-By-Step Directions From a Binary Tree Node to Another
You are given therootof abinary treewithnnodes. Each node is uniquely assigned a value from1ton. You are also given an integerstartValuerepresenting the value of the start nodes, and a different integerdestValuerepresenting the value of the d...原创 2022-04-01 11:26:11 · 302 阅读 · 0 评论 -
Binary Search Tree to Greater Sum Tree
Given therootof a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.As a reminder, abinary search treeis a tree ...原创 2022-03-28 01:18:53 · 386 阅读 · 0 评论 -
Maximum Level Sum of a Binary Tree
Given therootof a binary tree, the level of its root is1, the level of its children is2, and so on.Return thesmallestlevelxsuch that the sum of all the values of nodes at levelxismaximal.Example 1:Input: root = [1,7,0,7,-8,null,null]...原创 2022-02-20 14:22:59 · 205 阅读 · 0 评论 -
Diameter of N-Ary Tree
Given arootof anN-ary tree, you need to compute the length of the diameter of the tree.The diameter of an N-ary tree is the length of thelongestpath between any two nodes in the tree. This path may or may not pass through the root.(Nary-Tree input...原创 2022-02-20 08:33:07 · 212 阅读 · 0 评论 -
Print Binary Tree
Given therootof a binary tree, construct a0-indexedm x nstring matrixresthat represents aformatted layoutof the tree. The formatted layout matrix should be constructed using the following rules:Theheightof the tree isheightand the number of ...原创 2022-02-12 09:29:25 · 396 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Tree II
Given therootof a binary tree, returnthe lowest common ancestor (LCA) of two given nodes,pandq. If either nodeporqdoes not existin the tree, returnnull. All values of the nodes in the tree areunique.According to thedefinition of LCA on Wiki...原创 2022-01-02 08:29:58 · 351 阅读 · 0 评论 -
Count Good Nodes in Binary Tree
Given a binary treeroot, a nodeXin the tree is namedgoodif in the path from root toXthere are no nodes with a valuegreater thanX.Return the number ofgoodnodes in the binary tree.Example 1:Input: root = [3,1,4,3,null,1,5]Output: 4Expla...原创 2020-09-21 02:19:23 · 265 阅读 · 0 评论 -
Vertical Order Traversal of a Binary Tree
Given a binary tree, return thevertical ordertraversal of its nodesvalues.For each node at position(X, Y), its left and right children respectivelywill be at positions(X-1, Y-1)and(X+1, Y-1).Running a vertical line fromX = -infinitytoX = +in...原创 2020-08-10 13:02:13 · 228 阅读 · 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 评论 -
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 评论 -
Serialize and Deserialize BST
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 be...原创 2019-05-07 12:18:57 · 328 阅读 · 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 评论 -
Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.思路:注意current = preorder[pstart],不是preorder[0];preorder:...原创 2014-01-22 12:32:30 · 655 阅读 · 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 Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.思路:inorder: left, current, right;postorder, left, ri...原创 2014-01-22 13:02:32 · 571 阅读 · 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 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 评论 -
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 评论 -
Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is trivia...原创 2014-01-09 14:36:15 · 946 阅读 · 0 评论 -
PreOrder, InOrder, PostOrder 题型总结
Serialize and Deserialize BST思路:用preorder来构建string,因为preorder知道第一个就是root,找到后面大于root的点,就是right tree,注意null,用stringbuilder构建string。Time : O(N) serialize, O(N^2) deserialize;因为要循去找比root大的,T(n) = O(n) +...原创 2020-02-17 06:58:37 · 4184 阅读 · 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 评论 -
Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path.The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child co原创 2016-08-03 02:05:30 · 366 阅读 · 0 评论 -
All Elements in Two Binary Search Trees
Given two binary search treesroot1androot2.Return a list containingall the integersfromboth treessorted inascendingorder.Example 1:Input: root1 = [2,1,4], root2 = [1,0,3]Output: [0,1,1,2,3,4]思路:先用inorder traverse,把两个tree变成两个sorted queu...原创 2020-05-28 12:49:37 · 194 阅读 · 0 评论 -
Maximum Difference Between Node and Ancestor
Given therootof a binary tree, find the maximum valueVfor which there existsdifferentnodesAandBwhereV = |A.val - B.val|andAis an ancestor ofB.(A node A is an ancestor of B if either: any child of A is equal to B, or any child of A is an an...原创 2020-05-12 10:21:00 · 274 阅读 · 0 评论 -
Two Sum IV - Input is a BST
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.Example 1:Input: 5 / \ 3 6 / \ \...原创 2020-04-17 08:02:45 · 111 阅读 · 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 评论 -
Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of thelongestpath between any two nodes in a tree. This path may or may n...原创 2020-03-23 05:05:00 · 283 阅读 · 0 评论 -
Find Duplicate Subtrees
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of anyoneof them.Two trees are duplicate if they have the same struct...原创 2020-03-21 11:31:30 · 116 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant extr原创 2014-12-07 07:48:11 · 451 阅读 · 0 评论 -
Boundary of Binary Tree
Input: 1 \ 2 / \ 3 4Ouput:[1, 3, 4, 2]Explanation:The root doesn't have left subtree, so the root itself is left boundary.The leaves are node 3 and 4.The right boundary are no...原创 2020-02-21 14:04:36 · 252 阅读 · 0 评论 -
Binary Tree - Divide Conquer & Traverse 题型总结
place holder原创 2020-02-18 11:39:54 · 281 阅读 · 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 评论 -
Closest Binary Search Tree Value
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.Note:Given target value is a floating point. You are guaranteed to have only on...原创 2016-07-12 04:21:22 · 397 阅读 · 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 评论