<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)99:恢復(fù)二叉搜索樹(shù)

          共 2396字,需瀏覽 5分鐘

           ·

          2020-11-18 01:42

          算法的重要性,我就不多說(shuō)了吧,想去大廠(chǎng),就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問(wèn)題叫做?恢復(fù)二叉搜索樹(shù),我們先來(lái)看題面:

          https://leetcode-cn.com/problems/recover-binary-search-tree/

          You are given the root of a binary search tree (BST), where exactly two nodes of the tree were swapped by mistake. Recover the tree without changing its structure.


          Follow up: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?

          題意


          給你二叉搜索樹(shù)的根節(jié)點(diǎn) root ,該樹(shù)中的兩個(gè)節(jié)點(diǎn)被錯(cuò)誤地交換。請(qǐng)?jiān)诓桓淖兤浣Y(jié)構(gòu)的情況下,恢復(fù)這棵樹(shù)。

          進(jìn)階:使用 O(n) 空間復(fù)雜度的解法很容易實(shí)現(xiàn)。你能想出一個(gè)只使用常數(shù)空間的解決方案嗎?

          樣例


          解題


          這題思路和LeetCode098——驗(yàn)證二叉搜索樹(shù)中的思路二是一致的。

          對(duì)于一棵二叉搜索樹(shù)而言,其中序遍歷的結(jié)果是一個(gè)遞增序列。我們保存原二叉搜索樹(shù)中序遍歷的結(jié)果。再對(duì)該結(jié)果進(jìn)行排序后得到另一個(gè)序列,比較兩個(gè)序列中的不同的兩個(gè)值,即為需要交換的兩個(gè)錯(cuò)誤節(jié)點(diǎn)。

          時(shí)間復(fù)雜度是O(nlogn),其中n為樹(shù)中的節(jié)點(diǎn)個(gè)數(shù)。空間復(fù)雜度也是O(n)。


          public?class?Solution?{
          ?
          ????List list;
          ?
          ????public?void?recoverTree(TreeNode root)?{
          ????????list?= new?ArrayList<>();
          ????????inorderTraversal(root);
          ????????List tempList = new?ArrayList<>(list);
          ????????Collections.sort(tempList, new?Comparator() {
          ????????????@Override
          ????????????public?int?compare(TreeNode treeNode1, TreeNode treeNode2) {
          ????????????????return?treeNode1.val - treeNode2.val;
          ????????????}
          ????????});
          ????????List wrongList = new?ArrayList<>();
          ????????for(int?i = 0; i < list.size(); i++){
          ????????????if(list.get(i).val != tempList.get(i).val){
          ????????????????wrongList.add(i);
          ????????????}
          ????????}
          ????????change(list, wrongList.get(0), wrongList.get(1));
          ????}
          ?
          ????private?void?inorderTraversal(TreeNode root){
          ????????if(root == null){
          ????????????return;
          ????????}
          ????????inorderTraversal(root.left);
          ????????list.add(root);
          ????????inorderTraversal(root.right);
          ????}
          ?
          ????private?void?change(List list, int?i, int?j){
          ????????Integer temp = list.get(i).val;
          ????????list.get(i).val = list.get(j).val;
          ????????list.get(j).val = temp;
          ????}
          }


          這道題,大家還有沒(méi)有更好的解法呢?歡迎評(píng)論區(qū)討論 。
          好了,今天的文章就到這里,如果覺(jué)得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力。


          上期推文:

          LeetCode50-80題匯總,速度收藏!
          LeetCode刷題實(shí)戰(zhàn)81:搜索旋轉(zhuǎn)排序數(shù)組 II
          LeetCode刷題實(shí)戰(zhàn)82:刪除排序鏈表中的重復(fù)元素 II
          LeetCode刷題實(shí)戰(zhàn)83:刪除排序鏈表中的重復(fù)元素
          LeetCode刷題實(shí)戰(zhàn)84:?柱狀圖中最大的矩形
          LeetCode刷題實(shí)戰(zhàn)85:最大矩形
          LeetCode刷題實(shí)戰(zhàn)86:分隔鏈表
          LeetCode刷題實(shí)戰(zhàn)87:擾亂字符串
          LeetCode刷題實(shí)戰(zhàn)88:合并兩個(gè)有序數(shù)組
          LeetCode刷題實(shí)戰(zhàn)89:格雷編碼
          LeetCode刷題實(shí)戰(zhàn)90:子集 II
          LeetCode刷題實(shí)戰(zhàn)91:解碼方法
          LeetCode刷題實(shí)戰(zhàn)92:反轉(zhuǎn)鏈表 II
          LeetCode刷題實(shí)戰(zhàn)93:復(fù)原IP地址
          LeetCode刷題實(shí)戰(zhàn)94:二叉樹(shù)的中序遍歷
          LeetCode刷題實(shí)戰(zhàn)95:不同的二叉搜索樹(shù) II
          LeetCode刷題實(shí)戰(zhàn)96:不同的二叉搜索樹(shù)
          LeetCode刷題實(shí)戰(zhàn)97:交錯(cuò)字符串
          LeetCode刷題實(shí)戰(zhàn)98:驗(yàn)證二叉搜索樹(shù)

          瀏覽 34
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  香蕉视频99 | 国产美女日逼视频 | 欧美成年在线 | 美女啪啪av | 五月婷婷激情综合 |