算法训练(leetcode)休息日 | 589. N 叉树的前序遍历、590. N 叉树的后序遍历、100. 相同的树、572. 另一棵树的子树

刷题记录

  • [589. N 叉树的前序遍历](#589. N 叉树的前序遍历)
  • [590. N 叉树的后序遍历](#590. N 叉树的后序遍历)
  • [100. 相同的树](#100. 相同的树)
  • [572. 另一棵树的子树](#572. 另一棵树的子树)

589. N 叉树的前序遍历

leetcode题目地址

前序遍历访问左右子树改为访问孩子列表元素。

时间复杂度: 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 叉树的后序遍历

leetcode题目地址

和二叉树后续遍历类似,不多赘述。

时间复杂度: 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. 相同的树

leetcode题目地址

同时遍历两棵树。

时间复杂度: 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. 另一棵树的子树

leetcode题目地址

同上题类似。

时间复杂度: 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;
    }
};
相关推荐
im_AMBER1 小时前
数据结构 06 线性结构
数据结构·学习·算法
earthzhang20213 小时前
【1028】字符菱形
c语言·开发语言·数据结构·c++·算法·青少年编程
papership3 小时前
【入门级-算法-3、基础算法:二分法】
数据结构·算法
通信小呆呆3 小时前
收发分离多基地雷达椭圆联合定位:原理、算法与误差分析
算法·目标检测·信息与通信·信号处理
AA陈超5 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P05-08 UI 部件数据表
c++·游戏·ue5·游戏引擎·虚幻
纵有疾風起6 小时前
C++——类和对象(3)
开发语言·c++·经验分享·开源
丁浩6667 小时前
Python机器学习---2.算法:逻辑回归
python·算法·机器学习
承渊政道7 小时前
动态内存管理
c语言·c++·经验分享·c#·visual studio
孤独得猿8 小时前
聊天室项目开发——etcd的安装和使用
linux·服务器·c++·etcd
伏小白白白8 小时前
【论文精度-2】求解车辆路径问题的神经组合优化算法:综合展望(Yubin Xiao,2025)
人工智能·算法·机器学习