难度,简单
法一,递归
class Solution {
public:
vector<int> preorder(Node* root) {
if(root)
{
res.push_back(root->val);
for(auto node:(*root).children)
preorder(node);
}
return res;
}
private:
vector<int> res;
stack<Node *>
};
法二,正常迭代,借助栈
class Solution {
public:
vector<int> preorder(Node* root) {
if(!root)
return res;
temp.push(root);
while(!temp.empty())
{
Node *node = temp.top();
temp.pop();
if(node)
res.emplace_back(node->val);
else
continue;
if(!node->children.empty())
for(int i=node->children.size()-1;i>=0; i--)
temp.push(node->children[i]);
}
return res;
}
private:
vector<int> res;
stack<Node*> temp;
};
递归就不说了,这里附上非递归代码
https://leetcode-cn.com/problems/binary-tree-preorder-traversal/solution/miao-sha-quan-chang-ba-hou-lang-by-sonp/
中等
方法一:先中序遍历,得到一个序列,看该序列是否是递增的,属于常规思路
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(!root)
return true;
buildRes(root);
for(int i=0;i<res.size()-1;i++)
if(!(res[i] < res[i+1]))
return false;
return true;
}
void buildRes(TreeNode* root)
{
if(root)
{
buildRes(root->left);
res.emplace_back(root->val);
buildRes(root->right);
}
}
private:
vector<int> res;
};
方法二:
每一个节点的值 需要在lower 和 upper之间,遍历左子树时upper为根节点,遍历右子树时,lower为根节点 参考自官方题解
class Solution {
public:
bool helper(TreeNode* root, long long lower, long long upper) {
if (root == nullptr) return true;
if (root -> val <= lower || root -> val >= upper) return false;
return helper(root -> left, lower, root -> val) && helper(root -> right, root -> val, upper);
}
bool isValidBST(TreeNode* root) {
return helper(root, LONG_MIN, LONG_MAX);
}
};
中等
方法一,借助队列的两种写法,写法一是自己的做法
两种写法的思路是一样的,都是借助队列,不过我的写法是把非空节点入队,写法二是一股脑都入队,出队的时候再进行判断
8ms11.7MB
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(!root)
return res;
myque.push(root);
while(!myque.empty())
{
vector<int> temp;
int size = myque.size();
for(int i=0;i<size;i++)
{
temp.emplace_back(myque.front()->val);
if(myque.front()->left)
myque.push(myque.front()->left);
if(myque.front()->right)
myque.push(myque.front()->right);
myque.pop();
}
res.emplace_back(temp);
}
return res;
}
private:
queue<TreeNode *> myque;
vector<vector<int>> res;
};
写法二是参考题解稍作改进
4ms 11.8MB
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
/*if(!root)
return res;*/
myque.push(root);
while(!myque.empty())
{
vector<int> temp;
int size = myque.size();
while(size--)
{
TreeNode *node = myque.front();
myque.pop();
if(!node)
continue;
temp.emplace_back(node->val);
myque.push(node->left);
myque.push(node->right);
}
if(!temp.empty())
res.emplace_back(temp);
}
return res;
}
private:
queue<TreeNode *> myque;
vector<vector<int>> res;
};
方法二: 深度优先,上面的方法一是广度优先
但是正常来说,广度优先是顺着一条路走到底,然后再回头,这和先序遍历很像
但是这里怎么才能知道哪些节点是在同一层呢,通过变量level进行标记
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
buildRes(res, root, 0);
return res;
}
void buildRes(vector<vector<int>>& vec, TreeNode *root, int level)
{
//结束条件,遇到空节点
if(!root)
return;
//处理当前层逻辑:
如果是新的一层的节点,就对vector进行操作
if(level >= vec.size())
vec.emplace_back(vector<int>());
vec[level].emplace_back(root->val);
//开始下一层递归 对左右孩子进行 level要加一
buildRes(vec, root->left, level+1);
buildRes(vec, root->right, level+1);
}
private:
queue<TreeNode *> myque;
vector<vector<int>> res;
};