网页制作的目的和意义,专业的网站优化公司排名,软装设计师需要具备的能力,爬虫代理ip购买文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析#xff1a;注意不要落入下面你的陷阱#xff0c;笔者本来想左节点键值中间节点键值右节点键值即可… 文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析注意不要落入下面你的陷阱笔者本来想左节点键值中间节点键值右节点键值即可写出如下代码
class Solution2 {
public:// 1、输入参数 返回值为result,以引用方式传入void traversal_preOrder(TreeNode* cur, bool result) {// 2、终止条件if (cur NULL) return;if (cur-left cur-left-val cur-val) result 0;if (cur-right cur-right-val cur-val) result 0;// 3、单层递归逻辑if (result) traversal_preOrder(cur-left, result); // 左if (result) traversal_preOrder(cur-right, result); // 右}bool isValidBST(TreeNode* root) {bool result 1;traversal_preOrder(root, result);return result;}
};在leetcode执行时遇到下面的错误再次读题发现是所有左子树的键值小于中间节点键值所有右子树键值大于中间节点键值这个性质和中序遍历有所关联。如此一来我们就想到用中序遍历来做中序遍历数组是一个有序数组判断该数组是否有序即可。 程序如下
class Solution {
public:void traversal_midOrder(TreeNode* cur, vectorint vec) {if (cur NULL) return;traversal_midOrder(cur-left, vec); // 左vec.push_back(cur-val); // 中traversal_midOrder(cur-right, vec); // 右}bool isValidBST(TreeNode* root) {if (root NULL) return {};vectorint v;traversal_midOrder(root, v);for (int i 0; i v.size()-1; i) {if (v[i] v[i 1]) return false;}return true;}
};三、完整代码
# include iostream
# include vector
# include string
# include queue
using namespace std;// 树节点定义
struct TreeNode {int val;TreeNode* left;TreeNode* right;TreeNode() : val(0), left(nullptr), right(nullptr) {}TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};class Solution {
public:void traversal_midOrder(TreeNode* cur, vectorint vec) {if (cur NULL) return;traversal_midOrder(cur-left, vec); // 左vec.push_back(cur-val); // 中traversal_midOrder(cur-right, vec); // 右}bool isValidBST(TreeNode* root) {if (root NULL) return {};vectorint v;traversal_midOrder(root, v);for (int i 0; i v.size()-1; i) {if (v[i] v[i 1]) return false;}return true;}
};class Solution2 {
public:// 1、输入参数 返回值为result,以引用方式传入void traversal_preOrder(TreeNode* cur, bool result) {// 2、终止条件if (cur NULL) return;if (cur-left cur-left-val cur-val) result 0;if (cur-right cur-right-val cur-val) result 0;// 3、单层递归逻辑if (result) traversal_preOrder(cur-left, result); // 左if (result) traversal_preOrder(cur-right, result); // 右}bool isValidBST(TreeNode* root) {bool result 1;traversal_preOrder(root, result);return result;}
};// 前序遍历迭代法创建二叉树每次迭代将容器首元素弹出弹出代码还可以再优化
void Tree_Generator(vectorstring t, TreeNode* node) {if (!t.size() || t[0] NULL) return; // 退出条件else {node new TreeNode(stoi(t[0].c_str())); // 中if (t.size()) {t.assign(t.begin() 1, t.end());Tree_Generator(t, node-left); // 左}if (t.size()) {t.assign(t.begin() 1, t.end());Tree_Generator(t, node-right); // 右}}
}templatetypename T
void my_print(T v, const string msg)
{cout msg endl;for (class T::iterator it v.begin(); it ! v.end(); it) {cout *it ;}cout endl;
}templateclass T1, class T2
void my_print2(T1 v, const string str) {cout str endl;for (class T1::iterator vit v.begin(); vit v.end(); vit) {for (class T2::iterator it (*vit).begin(); it (*vit).end(); it) {cout *it ;}cout endl;}
}// 层序遍历
vectorvectorint levelOrder(TreeNode* root) {queueTreeNode* que;if (root ! NULL) que.push(root);vectorvectorint result;while (!que.empty()) {int size que.size(); // size必须固定, que.size()是不断变化的vectorint vec;for (int i 0; i size; i) {TreeNode* node que.front();que.pop();vec.push_back(node-val);if (node-left) que.push(node-left);if (node-right) que.push(node-right);}result.push_back(vec);}return result;
}int main()
{vectorstring t { 5, 4, NULL, NULL, 6, 3, NULL, NULL, 7, NULL, NULL }; // 前序遍历my_print(t, 目标树);TreeNode* root new TreeNode();Tree_Generator(t, root);vectorvectorint tree levelOrder(root);my_print2vectorvectorint, vectorint(tree, 目标树:);Solution s;bool result s.isValidBST(root);if (result) cout 是一棵二叉搜索树 endl;else cout 不是一棵二叉搜索树 endl;system(pause);return 0;
}end 文章转载自: http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.tfcwj.cn.gov.cn.tfcwj.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.fgkrh.cn.gov.cn.fgkrh.cn http://www.morning.zpyxl.cn.gov.cn.zpyxl.cn http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.thrcj.cn.gov.cn.thrcj.cn http://www.morning.frsbf.cn.gov.cn.frsbf.cn http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.ddzqx.cn.gov.cn.ddzqx.cn http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.rdmz.cn.gov.cn.rdmz.cn http://www.morning.yskhj.cn.gov.cn.yskhj.cn http://www.morning.wrbx.cn.gov.cn.wrbx.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.tntgc.cn.gov.cn.tntgc.cn http://www.morning.nqdkx.cn.gov.cn.nqdkx.cn http://www.morning.nkyqh.cn.gov.cn.nkyqh.cn http://www.morning.wxwall.com.gov.cn.wxwall.com http://www.morning.fppzc.cn.gov.cn.fppzc.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.lcbgf.cn.gov.cn.lcbgf.cn http://www.morning.lptjt.cn.gov.cn.lptjt.cn http://www.morning.wqmyh.cn.gov.cn.wqmyh.cn http://www.morning.lmqfq.cn.gov.cn.lmqfq.cn http://www.morning.jxjrm.cn.gov.cn.jxjrm.cn http://www.morning.qzdxy.cn.gov.cn.qzdxy.cn http://www.morning.nylbb.cn.gov.cn.nylbb.cn http://www.morning.mtqqx.cn.gov.cn.mtqqx.cn http://www.morning.ltrz.cn.gov.cn.ltrz.cn http://www.morning.qgfhr.cn.gov.cn.qgfhr.cn http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.srnth.cn.gov.cn.srnth.cn http://www.morning.wjrq.cn.gov.cn.wjrq.cn http://www.morning.wfhnz.cn.gov.cn.wfhnz.cn http://www.morning.xlyt.cn.gov.cn.xlyt.cn http://www.morning.drgmr.cn.gov.cn.drgmr.cn http://www.morning.mzgq.cn.gov.cn.mzgq.cn http://www.morning.cbynh.cn.gov.cn.cbynh.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.xkhxl.cn.gov.cn.xkhxl.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.hgscb.cn.gov.cn.hgscb.cn http://www.morning.fjshyc.com.gov.cn.fjshyc.com http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.lxyyp.cn.gov.cn.lxyyp.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.qrksj.cn.gov.cn.qrksj.cn http://www.morning.znpyw.cn.gov.cn.znpyw.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn http://www.morning.fcxt.cn.gov.cn.fcxt.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.dpflt.cn.gov.cn.dpflt.cn http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.kngqd.cn.gov.cn.kngqd.cn http://www.morning.txfxy.cn.gov.cn.txfxy.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.kjjbz.cn.gov.cn.kjjbz.cn http://www.morning.kgslc.cn.gov.cn.kgslc.cn http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.llllcc.com.gov.cn.llllcc.com http://www.morning.wkmrl.cn.gov.cn.wkmrl.cn http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.tmpsc.cn.gov.cn.tmpsc.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.zqwqy.cn.gov.cn.zqwqy.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.nwwzc.cn.gov.cn.nwwzc.cn http://www.morning.jxwhr.cn.gov.cn.jxwhr.cn