/**
* 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.
*/
ArrayList<Integer> result;
public ArrayList<Integer> searchRange(TreeNode root, int k1, int k2) {
result = new ArrayList<Integer>();
searchHelper(root, k1, k2);
return result;
}
private void searchHelper(TreeNode root, int k1, int k2) {
if (root == null) {
return;
}
if (root.val > k1) {
searchHelper(root.left, k1, k2);
}
if (root.val >= k1 && root.val <= k2) {
result.add(root.val);
}
if (root.val < k2) {
searchHelper(root.right, k1, k2);
}
}
}
if (condition) {
执行语句1;
} else {
执行语句2;
}
if (condition1) {
执行语句1;
}
if (condition2) {
执行语句2;
}
这种情况下,就是先执行语句1,进行一些操作,如果继续condition2,那么接着执行语句2. 非常容易搞错,一定要想清楚一个过程。