<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刷題實戰(zhàn)98:驗證二叉搜索樹

          共 1697字,需瀏覽 4分鐘

           ·

          2020-11-18 01:43

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

          今天和大家聊的問題叫做?驗證二叉搜索樹,我們先來看題面:

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

          Given a binary tree, determine if it is a valid binary search tree (BST).


          Assume a BST is defined as follows:


          The left subtree of a node contains only nodes with keys less than the node's key.

          The right subtree of a node contains only nodes with keys greater than the node's key.

          Both the left and right subtrees must also be binary search trees.


          題意

          給定一個二叉樹,判斷其是否是一個有效的二叉搜索樹。
          假設一個二叉搜索樹具有如下特征:
          • 節(jié)點的左子樹只包含小于當前節(jié)點的數(shù)。

          • 節(jié)點的右子樹只包含大于當前節(jié)點的數(shù)。

          • 所有左子樹和右子樹自身必須也是二叉搜索樹。


          樣例


          解題


          我們都知道二叉搜索樹:中序遍歷為嚴格的遞增序列。
          所以我們可以使用中序遍歷的遞歸遍歷這個二叉樹,并且實時更新當前中序遍歷到的最大值,如果正在判斷的節(jié)點的val不大于這個實時的最大值,那就說明這個二叉樹的中序遍歷不是嚴格遞增的,繼而判定這個二叉樹不是二叉搜索樹。

          class?Solution?{
          public:
          ??long long maxValue = LLONG_MIN;//中序遍歷的當前最大值
          ??bool isValidBST(TreeNode* root) {
          ????if?(root == NULL){
          ??????return?true;
          ????}
          ????if?(!isValidBST(root->left)){//首先判斷左子樹是否是搜索二叉樹
          ??????return?false;
          ????}
          ????if?(root->val <= maxValue){//如果發(fā)現(xiàn)不大于之前的遇到的最大值,說明中序遍歷不是嚴格的遞增
          ??????return?false;
          ????}
          ????maxValue = root->val;//更新中序遍歷到此時,遇到的最大值
          ????if?(!isValidBST(root->right)){//首先判斷右子樹是否是搜索二叉樹
          ??????return?false;
          ????}
          ????return?true;
          ??}
          };


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力。


          上期推文:

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

          瀏覽 35
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  国产黄色视频免费在线观看 | 亚洲精品乱码久久久久久自慰 | AAA网址| 好好日在线视频 | 色男人天堂|