国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode] Search Range in Binary Search Tree

draveness / 2104人閱讀

摘要:重點是根據(jù)的性質(zhì),先左后根最后右。另一重點是,函數(shù)和函數(shù)都要用的的參數(shù),記得在函數(shù)外層定義。

Problem

Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order.

Example

If k1 = 10 and k2 = 22, then your function should return [12, 20, 22].

    20
   /  
  8   22
 / 
4   12
Note

重點是:根據(jù)BST的性質(zhì),先左后根最后右。
另一重點是,helper函數(shù)和main函數(shù)都要用的的參數(shù),記得在main函數(shù)外層定義。

Solution
public class Solution {
    private ArrayList result;
    public ArrayList searchRange(TreeNode root, int k1, int k2) {
        // write your code here
        result = new ArrayList();
        Searchfunc(root, k1, k2);
        return result;
    }
    public void Searchfunc(TreeNode root, int k1, int k2) {
        if (root == null) {
            return;
        }
        if (root.val > k1) {
            Searchfunc(root.left, k1, k2);
            }
        if (root.val >= k1 && root.val <= k2) {
            result.add(root.val);
        }
        if (root.val < k2) {
            Searchfunc(root.right, k1, k2);
        }
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/65455.html

相關(guān)文章

  • [LintCode] Remove Node in Binary Search Tree [理解BS

    Problem Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You sho...

    陳江龍 評論0 收藏0
  • [LintCode] Insert Node in a Binary Search Tree

    摘要:建立兩個樹結(jié)點,先用找到在的位置,讓作為的根節(jié)點找到的位置后,指向。此時,用代替與連接就可以了。 Problem Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tr...

    Taste 評論0 收藏0
  • [LintCode] Binary Search Tree Iterator

    摘要:建立一個堆棧,先將最左邊的結(jié)點從大到小壓入棧,這樣的話,為了實現(xiàn)迭代即返回下一個的函數(shù)就要考慮右邊的結(jié)點。如此,實現(xiàn)函數(shù)。 Problem Design an iterator over a binary search tree with the following rules: Elements are visited in ascending order (i.e. an in-o...

    silencezwm 評論0 收藏0
  • [LintCode/LeetCode] Search for a Range [左右邊界法/一次循環(huán)

    摘要:首先,建立二元結(jié)果數(shù)組,起點,終點。二分法求左邊界當(dāng)中點小于,移向中點,否則移向中點先判斷起點,再判斷終點是否等于,如果是,賦值給。 Problem Given a sorted array of n integers, find the starting and ending position of a given target value. If the target is not...

    zhangrxiang 評論0 收藏0
  • Search Range in BST

    Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1 k2root >= left bound ----> search unti...

    godruoyi 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<