二分查找樹

A?Binary Search Tree?is a special form of a binary tree. The value in each node must be?greater than?(or equal to) any values in its?left subtree?but?less than?(or equal to) any values in its?right subtree.

二叉查找樹的基本操作

The strength of a BST is that you can perform all search, insertion and deletion operations in?O(h)?time complexity even in the worst case.

查找一個節(jié)點

According to the property of BST, for each node:

1. return the node if the target value is?equal to?the value of the node;

2. continue searching in the?left?subtree if the target value is?less than?the value of the node;

3. continue searching in the?right?subtree if the target value is?larger than?the value of the node.

LeetCode?700.?Search in a Binary Search Tree

插入一個新節(jié)點

Similar to our search strategy, for each node, we will:

1. search the left or right subtrees according to the relation of the value of the node and the value of our target node;

2. repeat STEP 1 until reaching an external node;

3. add the new node as its left or right child depending on the relation of the value of the node and the value of our target node.

In this way, we add a new node and maintain the property of BST.

LeetCode?701.?Insert into a Binary Search Tree

刪除一個指定節(jié)點

1. If the target node has?no child, we can simply remove the node.

2. If the target node has?one child, we can use its child to replace itself.

3. If the target node has?two children, replace the node with its in-order successor or predecessor node and delete that node.

LeetCode?450.?Delete Node in a BST

兩點注意:

1. 刪除節(jié)點的方式很多清寇;實際中朵逝,如果節(jié)點的值是個很復(fù)雜的對象盈魁,這種通過交換被刪除節(jié)點和其后繼的值的方式钦幔,在實際中可能代價會比較大,不如直接交換引用(如以下實現(xiàn))乐纸。

2. 如果被刪除節(jié)點的左右子樹均為空衬廷,那么可以將其左子樹作為其后繼節(jié)點的左子樹。不過這種方式有個缺點是汽绢,可能會加大樹的高度吗跋,從而使樹本身更加不平衡。以下實現(xiàn)中庶喜,通過改變左右子樹的引用小腊,返回其后繼節(jié)點作為新的根救鲤。

刪除指定節(jié)點
刪除根節(jié)點


其他相關(guān)算法

非遞歸中序遍歷二叉樹模板可以有效地解決一些問題久窟。

LeetCode 94.?Binary Tree Inorder Traversal

LeetCode?230.?Kth Smallest Element in a BST

LeetCode?98.?Validate Binary Search Tree

LeetCode?173.?Binary Search Tree Iterator


引用:

Learn one iterative inorder traversal, apply it to multiple tree questions

An easy-understanding O(h) time, O(1) space Java solution.

Iterative solution in Java, O(h) time and O(1) space

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市本缠,隨后出現(xiàn)的幾起案子斥扛,更是在濱河造成了極大的恐慌,老刑警劉巖丹锹,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件稀颁,死亡現(xiàn)場離奇詭異,居然都是意外死亡楣黍,警方通過查閱死者的電腦和手機(jī)匾灶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來租漂,“玉大人阶女,你說我怎么就攤上這事×ㄖ危” “怎么了秃踩?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長业筏。 經(jīng)常有香客問我憔杨,道長,這世上最難降的妖魔是什么蒜胖? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任消别,我火速辦了婚禮抛蚤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘寻狂。我一直安慰自己霉颠,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布荆虱。 她就那樣靜靜地躺著蒿偎,像睡著了一般。 火紅的嫁衣襯著肌膚如雪怀读。 梳的紋絲不亂的頭發(fā)上诉位,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天,我揣著相機(jī)與錄音菜枷,去河邊找鬼苍糠。 笑死,一個胖子當(dāng)著我的面吹牛啤誊,可吹牛的內(nèi)容都是我干的岳瞭。 我是一名探鬼主播,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼蚊锹,長吁一口氣:“原來是場噩夢啊……” “哼瞳筏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起牡昆,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤姚炕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后丢烘,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體柱宦,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年播瞳,在試婚紗的時候發(fā)現(xiàn)自己被綠了掸刊。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡赢乓,死狀恐怖忧侧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情骏全,我是刑警寧澤苍柏,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站姜贡,受9級特大地震影響试吁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一熄捍、第九天 我趴在偏房一處隱蔽的房頂上張望烛恤。 院中可真熱鬧,春花似錦余耽、人聲如沸缚柏。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽币喧。三九已至,卻和暖如春袱耽,著一層夾襖步出監(jiān)牢的瞬間杀餐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工朱巨, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留史翘,地道東北人。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓冀续,卻偏偏與公主長得像琼讽,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子洪唐,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,781評論 2 361

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,347評論 0 10
  • 零_dae9閱讀 225評論 0 0
  • 其實我是個拖延非常嚴(yán)重的人钻蹬,我這個拖延也不是一天兩天形成的,而是長年累月的積累桐罕。要一下子改好脉让,真的是不大可能桂敛。 從...
    微光Try閱讀 167評論 0 2