lintcode二叉查找树中搜索区间 java
来源:互联网 发布:陕西师范网络教育首页 编辑:程序博客网 时间:2024/06/15 23:00
给定两个值 k1 和 k2(k1 < k2)和一个二叉查找树的根节点。找到树中所有值在 k1 到 k2 范围内的节点。即打印所有x (k1 <= x <= k2) 其中 x 是二叉查找树的中的节点值。返回所有升序的节点值。
样例
如果有 k1 = 10
和 k2 = 22
, 你的程序应该返回 [12, 20, 22]
.
20 / \ 8 22 / \4 12
/** * 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: param root: The root of the binary search tree * @param k1: An integer * @param k2: An integer * @return: return: Return all keys that k1<=key<=k2 in ascending order */ public List<Integer> searchRange(TreeNode root, int k1, int k2) { if(root == null){ return null; } List<Integer> list = new ArrayList<Integer>(); if(root.val <=k2 && root.val >= k1){ List<Integer> retLeft = searchRange(root.left, k1, k2); if(null != retLeft){ list.addAll(retLeft); } list.add(root.val); List<Integer> retRight = searchRange(root.right, k1, k2); if(null != retRight){ list.addAll(retRight); } }else if(root.val > k2){ List<Integer> retLeft = searchRange(root.left, k1, k2); if(null != retLeft){ list.addAll(retLeft); } }else if(root.val < k1){ List<Integer> retRight = searchRange(root.right, k1, k2); if(null != retRight){ list.addAll(retRight); } } return list; }}
阅读全文
0 0
- lintcode二叉查找树中搜索区间 java
- Lintcode 二叉查找树中搜索区间
- lintcode-二叉查找树中搜索区间-11
- LintCode 11-二叉查找树中搜索区间
- LintCode 11 二叉查找树中搜索区间
- LintCode:M-二叉查找树中搜索区间
- LintCode-----11.二叉查找树中搜索区间
- (lintcode)第11题二叉查找树中搜索区间
- LINTCODE —— 二叉查找树中搜索区间
- Lintcode 11. 二叉查找树中搜索区间
- lintcode-二叉树中查找区间
- lintcode&九章算法——No.11 二叉查找树中搜索区间 ? 待解决
- 题目:二叉查找树中搜索区间
- 二叉查找树中搜索区间
- 二叉查找树中搜索区间
- 二叉查找树中搜索区间
- 二叉查找树中搜索区间
- 二叉查找树中搜索区间
- 梯度下降法及其Python实现
- 周报公示 软件编辑部 何佳呈
- Maven之OutOfMemoryError-yellowcong
- discuz 主题分类和分类信息详解
- [RK3128][Android 6.0] RTC设备注册及调用流程
- lintcode二叉查找树中搜索区间 java
- MySQL中的锁(表锁、行锁)
- Python学习笔记[第二周]
- 区间贪心问题
- java程序员的大数据之路(15):Pig Latin用户自定义函数
- 外部类、外部接口、类的内部类、类的内部接口、接口的内部类、接口的内部接口 修饰符
- 获取图片 渐变色
- java自定义注解
- Linux常用命令大全