Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order.
Example
If k1 = 10
and k2 = 22
, then your function should return[12, 20, 22]
.
20
/ \
8 22
/ \
4 12
这道题也是Google的面经题。
Solution:
public List<Integer> searchRange(TreeNode root, int k1, int k2) { List<Integer> result = new ArrayList<>(); help(result, root, k1, k2); return result; } private void help(List<Integer> result, TreeNode node, int k1, int k2) { if(node == null) return; if(node.val >= k1) { help(result, node.left, k1, k2); } if(node.val >= k1 && node.val <= k2) { result.add(node.val); } if(node.val > k2) return; help(result, node.right, k1, k2); }
相关推荐
java java_leetcode-99-recover-binary-search-tree
amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gzamoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz ...
javascript js_leetcode题解之109-convert-sorted-list-to-binary-search-tree.js
js js_leetcode题解之108-convert-sorted-array-to-binary-search-tree.js
js js_leetcode题解之106-construct-binary-tree-from-inorder
- Binary Tree(二叉树):涉及二叉树的遍历、深度、插入、搜索等基本操作。 - Binary Search(二分搜索):一种有效的查找算法,在有序数组中查找特定元素。 - Search in Rotated Sorted Array(旋转排序数组中的...
js js_leetcode题解之99-recover-binary-search-tree.js
js js_leetcode题解之98-validate-binary-search-tree.js
java java_leetcode-114-flatten-binary-tree-to-linked-list
java java_leetcode-105-construct-binary-tree-from-preorder-and-inorde
javascript js_leetcode题解之94-binary-tree-inorder-traversal.js
java java_leetcode-107-binary-tree-level-order-traversal
java java_leetcode-102-binary-tree-level-order-traversal
总之,"前端开源库-binary-search-tree"提供了一种在JavaScript环境中高效管理、操作和查询有序数据的方法,尤其是对于需要快速查找、插入和删除操作的场景,如实时搜索、动态数据过滤等。而AVL树的自平衡特性保证了...
java java_leetcode-110-balanced-binary-tree
python python_leetcode_109_Convert_Sorted_List_to_Binary_Search_Tree
java java_leetcode-maximum-depth-of-binary-tree
c语言入门 c语言_leetcode题解之1373_maximum_sum_bst_in_binary_tree
Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树
java java_leetcode-111-minimum-depth-of-binary-tree