zoukankan      html  css  js  c++  java
  • Leetcode题解(30)

    98. Validate Binary Search Tree

    题目

    分析:BST按照中序遍历之后所得到的序列是一个递增序列,因此可以按照这个思路,先中序遍历,保存好遍历的结果,然后在遍历一遍这个序列。判断其是否递增

    代码如下:

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     bool isValidBST(TreeNode* root) {
    13         if(NULL == root)
    14             return true;
    15         vector<int> res;
    16         inOrderTraversal(root,res);
    17         for(int i=1;i<res.size();i++)
    18         {
    19             if(res[i]<=res[i-1])
    20                 return false;
    21         }
    22         return true;
    23     }
    24     void inOrderTraversal(TreeNode* root,vector<int> &res)
    25     {
    26         if(NULL==root)
    27             return;
    28         inOrderTraversal(root->left,res);
    29         res.push_back(root->val);
    30         inOrderTraversal(root->right,res);
    31         
    32     }
    33 };

    上面的实现中,使用了一个vector,需要额外的O(n)空间,但是仔细一想,当在保存某一个之值时,只需要判断在它之前的那个数是否小于它,如果小于,则继续遍历,如果不小于,则可以返回false。

    代码如下,每次递归调用的时候,都将当前已经遍历的最大数字保存在key中即可

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     bool isValidBST(TreeNode* root) {
    13         if(NULL == root)
    14             return true;
    15         
    16         long long key = -2147483649;
    17         return inOrderTraversal(root,key);
    18         
    19     }
    20     bool inOrderTraversal(TreeNode* root,long long &key)
    21     {
    22         if(NULL==root)
    23             return true;
    24         bool flag = inOrderTraversal(root->left,key);
    25         if(flag == false)
    26             return false;
    27         if(root->val <= key)
    28         {
    29             return false;
    30         }
    31         key = root->val;
    32         flag = inOrderTraversal(root->right,key);
    33         if(flag == false)
    34             return false;
    35         return true;
    36         
    37 
    38             
    39         
    40     }
    41 };

     -----------------------------------------------------------------------------------------分割线---------------------------------------------------------------------------------

    100. Same Tree

    题目

    分析:

    判断两棵树结构是否相同,只需要递归判断其左右子树是否分别相同,并且当前节点的值也相同即可。

    代码如下:

     1 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11   public:
    12       bool isSameTree(TreeNode *p, TreeNode *q) {
    13           if (NULL == p && NULL == q)
    14           {
    15               return true;
    16           }
    17           else if (NULL != p && NULL == q)
    18           {
    19               return false;
    20           }
    21           else if (NULL == p && NULL != q)
    22           {
    23               return false;
    24           }
    25 
    26           if (p->val != q->val)
    27           {
    28               return false;
    29           }
    30 
    31           return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
    32 
    33 
    34       }
    35   };

     --------------------------------------------------------------------------------------分割线----------------------------------------------------------------------------------

    101. Symmetric Tree

    题目

    分析:

    这道题目,其实就是二叉树广度优先遍历的变形,只是每次入队列的方式和广度优先遍历算法中入队列的方式不同,详细见代码

     1 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12   bool isSymmetric(TreeNode *root) {
    13       queue<TreeNode *> myQueue;
    14       if (NULL == root)
    15       {
    16           return true;
    17       }
    18       if (NULL != root->left && NULL != root->right)
    19       {
    20           myQueue.push(root->left);
    21           myQueue.push(root->right);
    22       }
    23       else if (NULL == root->left && NULL == root->right)
    24       {
    25           return true;
    26       }
    27       else
    28           return false;
    29 
    30       TreeNode *temp1,*temp2;
    31 
    32       while(!myQueue.empty())
    33       {
    34           temp1 = myQueue.front();
    35           myQueue.pop();
    36           temp2 = myQueue.front();
    37           myQueue.pop();
    38           if (NULL == temp1 && NULL == temp2)
    39           {
    40               continue;
    41           }
    42           else if (NULL != temp1 && NULL == temp2)
    43           {
    44               return false;
    45           }
    46           else if (NULL == temp1 && NULL != temp2)
    47           {
    48               return false;
    49           }
    50           else if (temp1->val == temp2->val)
    51           {
    52               myQueue.push(temp1->left);//左节点
    53               myQueue.push(temp2->right);//右节点
    54               myQueue.push(temp1->right);
    55               myQueue.push(temp2->left);                        
    56           }
    57           else
    58               return false;
    59 
    60       }
    61       return true;
    62 
    63   }
    64 };

     ----------------------------------------------------------------------------------------------分割线----------------------------------------------------------------------------

    102. Binary Tree Level Order Traversal

    题目

    分析:

    二叉树的广度优先遍历,代码如下:

     1 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     vector<vector<int> > levelOrder(TreeNode *root) {
    13         vector<vector<int> > res;
    14         stack<vector<int> > tempStack;
    15         
    16         vector<int> temp;
    17         TreeNode *pNode=NULL;
    18         temp.clear();
    19         res.clear();
    20         if (NULL == root)
    21         {
    22             return res;
    23         }
    24 
    25         queue<TreeNode *> myQue;
    26         myQue.push(root);
    27         while (!myQue.empty())
    28         {
    29             myQue.push(NULL);//通过NULL标志进行分层
    30             temp.clear();
    31             pNode = myQue.front();
    32             myQue.pop();
    33             while (NULL != pNode)
    34             {
    35                 temp.push_back(pNode->val);
    36                 if (NULL != pNode->left)
    37                 {
    38                     myQue.push(pNode->left);
    39                 }
    40                 if (NULL != pNode->right)
    41                 {
    42                     myQue.push(pNode->right);
    43                 }
    44                 pNode = myQue.front();
    45                 myQue.pop();
    46 
    47             }
    48             
    49             res.push_back(temp);
    50             
    51         }
    52         return res;       
    53     }
    54 };
  • 相关阅读:
    duilib listUI滚动列表的时候回出现在LIst外面显示的情况
    VS中关于字节大小的总结
    linux系统上搭建egret构建环境(针对5.3.x 版以上)
    android studio 2.0 Gradle HttpProxy 设置
    低压差稳压器--AMS1117芯片简介
    车联网技术简介
    MATLAB语音信号处理
    汇编语言系列Ⅳ 实现发出各种声音
    汇编语言系列Ⅲ 实现字符串操作
    汇编语言系列Ⅱ 实现简单数学运算
  • 原文地址:https://www.cnblogs.com/LCCRNblog/p/5179523.html
Copyright © 2011-2022 走看看