刷题记录
- [589. N 叉树的前序遍历](#589. N 叉树的前序遍历)
- [590. N 叉树的后序遍历](#590. N 叉树的后序遍历)
- [100. 相同的树](#100. 相同的树)
- [572. 另一棵树的子树](#572. 另一棵树的子树)
589. N 叉树的前序遍历
前序遍历访问左右子树改为访问孩子列表元素。
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)
递归
cpp
// c++
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
void Order(Node* root, vector<int> &result){
if(!root) return;
result.emplace_back(root->val);
for(int i=0; i<root->children.size(); i++)
Order(root->children[i], result);
}
vector<int> preorder(Node* root) {
vector<int> result;
Order(root, result);
return result;
}
};
非递归
非递归前序遍历需要注意入栈顺序,栈是先进后出,所以要从右向左入栈孩子节点。
cpp
// c++
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<int> preorder(Node* root) {
if(!root) return {};
stack<Node*> st;
vector<int> result;
st.push(root);
while(!st.empty()){
root = st.top();
st.pop();
result.emplace_back(root->val);
for(int i=root->children.size()-1; i>=0; i--){
st.push(root->children[i]);
}
}
return result;
}
};
590. N 叉树的后序遍历
和二叉树后续遍历类似,不多赘述。
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)
cpp
// c++
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
void Order(Node* root, vector<int> &result){
if(!root) return;
for(int i=0; i<root->children.size(); i++)
Order(root->children[i], result);
result.emplace_back(root->val);
}
vector<int> postorder(Node* root) {
vector<int> result;
Order(root, result);
return result;
}
};
100. 相同的树
同时遍历两棵树。
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)
cpp
// c++
/**
* Definition for a binary tree node.
* 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:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(!p && !q) return true;
if(!p || !q) return false;
if(p->val!=q->val) return false;
bool left = isSameTree(p->left, q->left);
bool right = isSameTree(p->right, q->right);
return left && right;
}
};
572. 另一棵树的子树
同上题类似。
时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)
cpp
// c++
/**
* Definition for a binary tree node.
* 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:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(!p && !q) return true;
if(!p || !q) return false;
if(p->val!=q->val) return false;
bool left = isSameTree(p->left, q->left);
bool right = isSameTree(p->right, q->right);
return left && right;
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if(!root && !subRoot) return true;
if(!subRoot || !root) return false;
if(root->val == subRoot->val) {
bool isSame = isSameTree(root, subRoot);
if(isSame) return isSame;
}
bool left = isSubtree(root->left, subRoot);
bool right = isSubtree(root->right, subRoot);
return left || right;
}
};