二叉查找树中搜索区间
来源:互联网 发布:python android接口 编辑:程序博客网 时间:2024/05/29 03:15
二叉查找树中搜索区间
Python
"""Definition of TreeNode:class TreeNode: def __init__(self, val): self.val = val self.left, self.right = None, None"""class Solution: """ @param root: The root of the binary search tree. @param k1 and k2: range k1 to k2. @return: Return all keys that k1<=key<=k2 in ascending order. """ def searchRange(self, root, k1, k2): # write your code here L = [] self.preOrderVisit(root, k1, k2, L) L = sorted(L) return L def preOrderVisit(self, root, k1, k2, L): if root == None: return else: if root.val >= k1 and root.val <= k2: L.append(root.val) self.preOrderVisit(root.left, k1, k2, L) self.preOrderVisit(root.right, k1, k2, L)
java
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /** * @param root: The root of the binary search tree. * @param k1 and k2: range k1 to k2. * @return: Return all keys that k1<=key<=k2 in ascending order. */ public ArrayList<Integer> searchRange(TreeNode root, int k1, int k2) { // write your code here ArrayList<Integer> L = new ArrayList(); preOrderVisit(root, L, k1, k2); Collections.sort(L); return L; } private static void preOrderVisit(TreeNode root, ArrayList<Integer> L, int k1, int k2){ if(root == null){ return; } else{ if(root.val >= k1 && root.val <=k2){ L.add(root.val); } preOrderVisit(root.left, L, k1, k2); preOrderVisit(root.right, L, k1, k2); } }}
0 0
- 题目:二叉查找树中搜索区间
- 二叉查找树中搜索区间
- 二叉查找树中搜索区间
- 二叉查找树中搜索区间
- Lintcode 二叉查找树中搜索区间
- 二叉查找树中搜索区间
- 二叉查找树中搜索区间
- 二叉查找树搜索区间
- lintcode-二叉查找树中搜索区间-11
- LintCode 11-二叉查找树中搜索区间
- LintCode 11 二叉查找树中搜索区间
- LintCode:M-二叉查找树中搜索区间
- LintCode-----11.二叉查找树中搜索区间
- (lintcode)第11题二叉查找树中搜索区间
- LINTCODE —— 二叉查找树中搜索区间
- lintcode二叉查找树中搜索区间 java
- Lintcode 11. 二叉查找树中搜索区间
- 11.Search Range in Binary Search Tree-二叉查找树中搜索区间(中等题)
- Mysql主从复制
- mysql存储引擎(一)
- 线程的问题
- css盒子模型的示例
- Android中View绘制流程
- 二叉查找树中搜索区间
- mysql connector c++测试
- .NET程序集强命名删除与再签名技术 源代码剖析
- UIButton文字居左显示
- ZOJ 3878-Convert QWERTY to Dvorak【模拟】
- KMeans算法检测网络异常入侵
- 极限学习机(ELM)的使用
- The 3n + 1 problem
- Floyd、Dijkstra、Bellman-Ford、SPFA的比较