11
来源:互联网 发布:java 手动编译web项目 编辑:程序博客网 时间:2024/05/16 11:06
2017.9.14
二叉搜索树的前序遍历就是有序数列。
/** * 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) { // write your code here LinkedList<Integer> res = new LinkedList<Integer>(); if(root == null){ return res; } // 其实也就是二叉树的前序遍历。 LinkedList<TreeNode> stack = new LinkedList<TreeNode>(); TreeNode bt = root; while(bt != null || !stack.isEmpty()){ while(bt !=null){ stack.push(bt); bt = bt.left; } bt = stack.pop(); if(bt.val > k2){ return res; } if(bt.val >= k1){ res.add(bt.val); } bt = bt.right; } return res; }}
阅读全文
0 0
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- 11
- jQuery中的class操作
- 网易2017春招[编程题]集合@Java
- 云端车牌识别软件简介
- 牛客网-DNA最长片段
- simplejson之JSON转化为对象-yellowcong
- 11
- RecyclerView的使用
- 使用react-router-dom优雅的实现页面(路由)跳转, 而且保持当前页面状态
- 算法系列之--Javascript和Kotlin的插入算法(原)
- strstr的用法
- 碰瓷碰出新高度?团伙作案有预谋,一言不合就碰你,还有这种操作?
- Oracle基础教程-第十章 安全管理
- 层次查询、行列转换学习笔记
- 删除和恢复 一起写在一个JS方法中