首页 技术 正文
技术 2022年11月9日
0 收藏 358 点赞 3,515 浏览 3902 个字

98. Validate Binary Search Tree

题目

Leetcode题解(30)

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

代码如下:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(NULL == root)
return true;
vector<int> res;
inOrderTraversal(root,res);
for(int i=;i<res.size();i++)
{
if(res[i]<=res[i-])
return false;
}
return true;
}
void inOrderTraversal(TreeNode* root,vector<int> &res)
{
if(NULL==root)
return;
inOrderTraversal(root->left,res);
res.push_back(root->val);
inOrderTraversal(root->right,res); }
};

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

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

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(NULL == root)
return true; long long key = -;
return inOrderTraversal(root,key); }
bool inOrderTraversal(TreeNode* root,long long &key)
{
if(NULL==root)
return true;
bool flag = inOrderTraversal(root->left,key);
if(flag == false)
return false;
if(root->val <= key)
{
return false;
}
key = root->val;
flag = inOrderTraversal(root->right,key);
if(flag == false)
return false;
return true; }
};

—————————————————————————————–分割线———————————————————————————

100. Same Tree

题目

Leetcode题解(30)

分析:

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

代码如下:

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if (NULL == p && NULL == q)
{
return true;
}
else if (NULL != p && NULL == q)
{
return false;
}
else if (NULL == p && NULL != q)
{
return false;
} if (p->val != q->val)
{
return false;
} return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right); }
};

————————————————————————————–分割线———————————————————————————-

101. Symmetric Tree

题目

Leetcode题解(30)

分析:

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

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode *root) {
queue<TreeNode *> myQueue;
if (NULL == root)
{
return true;
}
if (NULL != root->left && NULL != root->right)
{
myQueue.push(root->left);
myQueue.push(root->right);
}
else if (NULL == root->left && NULL == root->right)
{
return true;
}
else
return false; TreeNode *temp1,*temp2; while(!myQueue.empty())
{
temp1 = myQueue.front();
myQueue.pop();
temp2 = myQueue.front();
myQueue.pop();
if (NULL == temp1 && NULL == temp2)
{
continue;
}
else if (NULL != temp1 && NULL == temp2)
{
return false;
}
else if (NULL == temp1 && NULL != temp2)
{
return false;
}
else if (temp1->val == temp2->val)
{
myQueue.push(temp1->left);//左节点
myQueue.push(temp2->right);//右节点
myQueue.push(temp1->right);
myQueue.push(temp2->left);
}
else
return false; }
return true; }
};

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

102. Binary Tree Level Order Traversal

题目

Leetcode题解(30)

分析:

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

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > levelOrder(TreeNode *root) {
vector<vector<int> > res;
stack<vector<int> > tempStack; vector<int> temp;
TreeNode *pNode=NULL;
temp.clear();
res.clear();
if (NULL == root)
{
return res;
} queue<TreeNode *> myQue;
myQue.push(root);
while (!myQue.empty())
{
myQue.push(NULL);//通过NULL标志进行分层
temp.clear();
pNode = myQue.front();
myQue.pop();
while (NULL != pNode)
{
temp.push_back(pNode->val);
if (NULL != pNode->left)
{
myQue.push(pNode->left);
}
if (NULL != pNode->right)
{
myQue.push(pNode->right);
}
pNode = myQue.front();
myQue.pop(); } res.push_back(temp); }
return res;
}
};
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905