算法训练(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;
    }
};
相关推荐
卡戎-caryon14 分钟前
【数据结构】06.栈&&队列
c语言·数据结构·算法·链表
山脚ice24 分钟前
【CT】LeetCode手撕—704. 二分查找
算法·leetcode
国中之林28 分钟前
【qt】如何获取网卡的IP地址?
服务器·c++·qt·网络协议·学习·tcp/ip
贱贱的剑1 小时前
【算法】选择排序
算法·rust·排序算法
瑜陀1 小时前
2024.06.30 刷题日记
数据结构·算法·leetcode
Star Patrick1 小时前
*算法训练(leetcode)第二十天 | 39. 组合总和、40. 组合总和 II、131. 分割回文串
c++·算法·leetcode
光久li1 小时前
【算法刷题 | 动态规划14】6.28(最大子数组和、判断子序列、不同的子序列)
算法·动态规划
飘然渡沧海1 小时前
gbk,ucs-2转中文
java·开发语言·算法
raykingl1 小时前
154. 寻找旋转排序数组中的最小值 II(困难)
java·python·算法·二分查找
raykingl1 小时前
69. x 的平方根(简单)
java·python·算法·二分查找