leetcode递归
Bryan要加油
这个作者很懒,什么都没留下…
展开
-
998. Maximum Binary Tree II
ProblemWe are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree.Just as in the previous problem, the given tree was constructed f...原创 2020-03-23 20:29:42 · 125 阅读 · 0 评论 -
951. Flip Equivalent Binary Trees
ProblemFor a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.A binary tree X is flip equivalent to a binary tree Y if and only i...原创 2020-03-23 19:50:52 · 146 阅读 · 0 评论 -
889. Construct Binary Tree from Preorder and Postorder Traversal
ProblemReturn any binary tree that matches the given preorder and postorder traversals.Values in the traversals pre and post are distinct positive integers.Note:1 <= pre.length == post.length ...原创 2020-03-22 21:28:11 · 198 阅读 · 0 评论 -
1325. Delete Leaves With a Given Value
ProblemGiven a binary tree root and an integer target, delete all the leaf nodes with value target.Note that once you delete a leaf node with value target, if it’s parent node becomes a leaf node an...原创 2020-03-22 20:39:38 · 174 阅读 · 0 评论 -
1261. Find Elements in a Contaminated Binary Tree
ProblemGiven a binary tree with the following rules:root.val == 0If treeNode.val == x and treeNode.left != null, then treeNode.left.val == 2 * x + 1If treeNode.val == x and treeNode.right != null,...原创 2020-03-21 23:21:05 · 201 阅读 · 0 评论 -
894. All Possible Full Binary Trees
ProblemA full binary tree is a binary tree where each node has exactly 0 or 2 children.Return a list of all possible full binary trees with N nodes. Each element of the answer is the root node of o...原创 2020-03-21 22:59:41 · 259 阅读 · 0 评论 -
1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
ProblemGiven two binary trees original and cloned and given a reference to a node target in the original tree.The cloned tree is a copy of the original tree.Return a reference to the same node in t...原创 2020-03-21 22:20:17 · 532 阅读 · 0 评论 -
814. Binary Tree Pruning
ProblemWe are given the head node root of a binary tree, where additionally every node’s value is either a 0 or a 1.Return the same tree where every subtree (of the given tree) not containing a 1 ha...原创 2020-03-18 20:05:42 · 190 阅读 · 0 评论 -
1008. Construct Binary Search Tree from Preorder Traversal
ProblemReturn the root node of a binary search tree that matches the given preorder traversal.(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has...原创 2020-03-12 22:42:41 · 115 阅读 · 0 评论 -
701. Insert into a Binary Search Tree
ProblemGiven the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed...原创 2020-03-12 22:05:53 · 110 阅读 · 0 评论 -
652. Find Duplicate Subtrees
ProblemGiven a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them.Two trees are duplicate if they have the same...原创 2020-03-10 23:35:13 · 206 阅读 · 0 评论 -
1305. All Elements in Two Binary Search Trees
ProblemGiven two binary search trees root1 and root2.Return a list containing all the integers from both trees sorted in ascending order.Example1Input: root1 = [2,1,4], root2 = [1,0,3]Output: [...原创 2020-03-09 23:19:26 · 485 阅读 · 0 评论 -
1315. Sum of Nodes with Even-Valued Grandparent
ProblemGiven a binary tree, return the sum of values of nodes with even-valued grandparent. (A grandparent of a node is the parent of its parent, if it exists.)If there are no nodes with an even-va...原创 2020-03-09 22:48:30 · 303 阅读 · 0 评论 -
654. Maximum Binary Tree
ProblemGiven 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 constru...原创 2020-03-09 20:49:42 · 133 阅读 · 0 评论 -
508. Most Frequent Subtree Sum
ProblemGiven the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node ...原创 2020-03-08 11:19:23 · 89 阅读 · 0 评论 -
面试题 04.06. Successor LCCI
ProblemWrite an algorithm to find the “next” node (i.e., in-order successor) of a given node in a binary search tree.Return null if there’s no “next” node for the given node.Example1Example2Sol...原创 2020-03-06 20:04:09 · 216 阅读 · 0 评论 -
面试题 04.10. 检查子树
ProblemT1 and T2 are two very large binary trees, with T1 much bigger than T2. Create an algorithm to determine if T2 is a subtree of T1.A tree T2 is a subtree of T1 if there exists a node n in T1 s...原创 2020-03-06 19:29:57 · 320 阅读 · 0 评论 -
面试题26. 树的子结构
Problem输入两棵二叉树A和B,判断B是不是A的子结构。(约定空树不是任意一个树的子结构)B是A的子结构, 即 A中有出现和B相同的结构和节点值。Example1输入:A = [1,2,3], B = [3,1]输出:falseExample2输入:A = [3,4,5,1,2], B = [4,1]输出:trueSolution这道题一开始没想明白,其实还是常规的...原创 2020-03-05 20:37:30 · 222 阅读 · 1 评论 -
938. Range Sum of BST
ProblemGiven the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).The binary search tree is guaranteed to have unique values.Example1...原创 2020-03-02 21:08:29 · 140 阅读 · 0 评论 -
965. Univalued Binary Tree
ProblemA binary tree is univalued if every node in the tree has the same value.Return true if and only if the given tree is univalued.Example1Input: [1,1,1,1,1,null,1]Output: trueExample2I...原创 2020-03-02 21:03:30 · 114 阅读 · 0 评论 -
993. Cousins in Binary Tree
ProblemIn a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.Two nodes of a binary tree are cousins if they have the same depth, but have different parent...原创 2020-03-02 21:00:36 · 200 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal
ProblemGiven a binary tree, return the inorder traversal of its nodes’ values.ExampleSolutionSolution1最最基本的递归解/** * Definition for a binary tree node. * struct TreeNode { * int val; * ...原创 2020-03-02 20:50:18 · 90 阅读 · 0 评论 -
538. Convert BST to Greater Tree && 1038. Binary Search Tree to Greater Sum Tree
Problem538Given 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 sum of all keys greater than the original key in ...原创 2020-03-01 20:05:46 · 202 阅读 · 0 评论 -
530. Minimum Absolute Difference in BST
ProblemGiven a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.ExampleSolutionBST中差值绝对值最小,只能出现在相邻节点。本题与501题是类似的。/** * Definiti...原创 2020-03-01 19:50:58 · 99 阅读 · 0 评论 -
501. Find Mode in Binary Search Tree
ProblemGiven a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.Assume a BST is defined as follows:The left subtree of a node c...原创 2020-03-01 16:33:04 · 132 阅读 · 0 评论 -
1022. Sum of Root To Leaf Binary Numbers
ProblemGiven a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 ->...原创 2020-02-27 23:11:43 · 196 阅读 · 0 评论 -
面试题54. 二叉搜索树的第k大节点
Problem给定一棵二叉搜索树,请找出其中第k大的节点。Example 1Example 2SolutionSolution1利用BST中序遍历为递增序列的特性,将遍历结果储存在数组中,然后从后向前找到第k个数。Solution2BST反向中序遍历(右->中->左)的结果是递减序列。所以在反向中序过程中,记录当前遍历的是第几个节点,当遍历到第k个时,就是结果。/...原创 2020-02-26 23:23:12 · 427 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree && 面试题55 - I. 二叉树的深度 LCOF
ProblemGiven 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.Note: A leaf is a node with no c...原创 2020-02-26 23:04:33 · 172 阅读 · 0 评论 -
606. Construct String from Binary Tree
ProblemYou need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.The null node needs to be represented by empty parenthesis pair “()”. A...原创 2020-02-20 23:19:01 · 165 阅读 · 0 评论 -
617. Merge Two Binary Trees
ProblemGiven 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 bina...原创 2020-02-19 22:22:03 · 114 阅读 · 0 评论 -
653. Two Sum IV - Input is a BST
ProblemGiven 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.Example1Example2Solution二叉搜索树中序遍历的结果,是...原创 2020-02-16 22:40:34 · 174 阅读 · 0 评论 -
671. 二叉树中第二小的节点
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node’s va...原创 2020-02-14 23:22:03 · 189 阅读 · 0 评论 -
700. Search in a Binary Search Tree
ProblemGiven the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the given value. Return the subtree rooted with that node. If s...原创 2020-02-13 22:24:45 · 107 阅读 · 0 评论 -
783. Minimum Distance Between BST Nodes
ProblemGiven a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.ExampleInput: root = [4,2,6,1,3,null,null]O...原创 2020-02-13 22:09:42 · 143 阅读 · 0 评论 -
872. Leaf-Similar Trees
ProblemConsider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence.For example, in the given tree above, the leaf value sequence is (6...原创 2020-02-12 23:22:33 · 125 阅读 · 0 评论 -
563. Binary Tree Tilt
ProblemGiven a binary tree, return the tilt of the whole tree.The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right s...原创 2020-01-16 20:24:32 · 261 阅读 · 0 评论 -
559. Maximum Depth of N-ary Tree
ProblemGiven a n-ary 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.Nary-Tree input serialization is ...原创 2020-01-16 20:06:19 · 215 阅读 · 0 评论 -
543. Diameter of Binary Tree(Leetcode每日一题-2020.03.10)
ProblemGiven 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 the longest path between any two nodes in a tree. This path may ...原创 2020-01-16 19:47:29 · 148 阅读 · 0 评论 -
437. Path Sum III
ProblemYou 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 ...原创 2020-01-15 19:33:14 · 132 阅读 · 0 评论 -
404. Sum of Left Leaves
ProblemFind the sum of all left leaves in a given binary tree.ExampleThere are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.Solution如果我们是要求整个二叉树上所有节点的和,可以很快...原创 2020-01-15 19:24:20 · 129 阅读 · 0 评论