lintcode-二叉树中查找区间

xiaoxiao2021-02-28  84

/** * 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://www.6miu.com/read-44642.html

最新回复(0)