lintcode-二叉树中查找区间

/** * 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 */
     ArrayList<Integer> result = new ArrayList<>();

    public List<Integer> searchRange(TreeNode root, int k1, int k2) {
        // write your code here
        inOrder(root, k1, k2);
        return result;
    }

    private void inOrder(TreeNode root, int k1, int k2) {
        if(root == null)return;
        inOrder(root.left, k1, k2);
        if(root.val <= k2 && root.val >= k1) {
            result.add(root.val);
        }
        inOrder(root.right, k1, k2);
    }
}
    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/qq_29728627/article/details/77748595
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞