• LintCode 11. Search Range in Binary Search Tree


    import java.util.ArrayList;
    import java.util.List;
    
    public class SearchRange {
        /**
         * 11. Search Range in Binary Search Tree
         * @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
         * <p>
         * Search Range in Binary Search Tree
         * Given a binary search tree and a range [k1, k2], return all elements in the given range.
         * <p>
         * 样例
         * 如果有 k1 = 10 和 k2 = 22, 你的程序应该返回 [12, 20, 22].
         * <p>
         *     {20,1,40,#,#,35}
         *     20
         *  1    40
         *     35
         *            1
         *          /  
         *        2     3
         *      /    / 
         *    4   5  6   7
         *   /
         * 8
         */
    
        public List<Integer> searchRange(TreeNode root, int k1, int k2) {
            // write your code here
            List<Integer> list = new ArrayList<>();
            inOrderTraverse(root, k1, k2, list);
            return list;
        }
    
        public void inOrderTraverse(TreeNode root, int k1, int k2, List<Integer> list) {
            if (root != null) {
                if (root.left != null) {
                    inOrderTraverse(root.left, k1, k2, list);
                }
                if (root.val >= k1 && root.val <= k2) {
                    list.add(root.val);
                }
                if (root.right != null) {
                    inOrderTraverse(root.right, k1, k2, list);
                }
            }
        }
    }
    
  • 相关阅读:
    hdu 6049 Sdjpx Is Happy
    L2-012. 关于堆的判断
    L2-010. 排座位
    L2-009. 抢红包
    L2-007. 家庭房产
    L2-008. 最长对称子串
    L2-011. 玩转二叉树
    l2-006 树的遍历
    l2-005
    l1-20 帅到没朋友
  • 原文地址:https://www.cnblogs.com/wei1/p/9582058.html
Copyright © 2020-2023  润新知