• (十三)树【C++刷题】


    二叉树的层序遍历

    Leetcode:102,https://leetcode-cn.com/problems/binary-tree-level-order-traversal/

    1.问题描述

    给定一个二叉树,返回按层序遍历得到的节点值。(即逐层的,从左到右访问所有节点)。

    2.输入输出

    • Input:[3, 9, 20, null, null, 15, 7]
    • Output:[[3], [9, 20], [15, 7]]

    3.算法分析

    利用队列先进先出的特性完成,期间通过一个size来记录每层的节点数,从而判断出一层。

    • 时间复杂度:O(N)
    • 空间复杂度:O(N)

    4.编程实现

    #include <iostream>
    #include <vector>
    #include <stack>
    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:
    	vector<vector<int>> levelOrder(TreeNode *root) {
            vector<vector<int>> level;
            if (!root) return level;
            queue<TreeNode *> que;
            que.push(root);
    
            while (!que.empty()) {
                vector<int> tmp;
                int len = que.size();
                for (int i = 0; i < len; i++){
                	TreeNode *node = que.front();
                	que.pop();
                	tmp.push_back(node->val);
                	if (node->left) que.push(node->left);
                	if (node->right) que.push(node->right);
                }
                level.push_back(tmp);
            }
            
            return level;
    	}
    };
    
    int main() {
        TreeNode *root = new TreeNode(1);
        root->right = new TreeNode(2);
        root->right->left = new TreeNode(3);
        
        Solution sol;
        for (auto val: sol.levelOrder(root))  {
            cout << val << " ";
        }
        
        return 0;
    }
    

    二叉树的锯齿形层序遍历

    Leetcode:https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/

    1.问题描述

    给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

    2.输入输出

    • Input:[3,9,20,null,null,15,7]
    • Output:[[3],[20,9],[15,7]]

    3.算法分析

    采用队列,层序遍历,记录每一层大小后直接在对应索引处赋值

    4.编程实现

    #include <iostream>
    #include <queue>
    #include <vector>
    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) {}
    };
    
    class Solution {
    public:
        vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
            vector<vector<int>> res;
            queue<TreeNode *> q;
            if (root) q.push(root);
            
            bool lr = true;
            while (!q.empty()) {
                int size = q.size();
                vector<int> level(size, 0);
                while (size--) {
                    root = q.front();
                    q.pop();
                    level[lr ? level.size() - size - 1 : size] = root->val;
                    if (root->left) q.push(root->left);
                    if (root->right) q.push(root->right);
                }
                res.push_back(move(level));
                lr = !lr;
            }
            
            return res;
        }
    };
    
    int main() {
        int val;
        Solution sol;
        TreeNode *root = new TreeNode(3);
        root->left = new TreeNode(9);
        root->right = new TreeNode(20);
        root->right->left = new TreeNode(15);
        root->right->right = new TreeNode(7);
        
        cout << sol.zigzagLevelOrder(root)[1][1] << endl;
    }
    

    include

    include

    include

    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:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    if (root == q || root == p || root == NULL) return root;
    TreeNode* left = lowestCommonAncestor(root->left, p, q);
    TreeNode* right = lowestCommonAncestor(root->right, p, q);
    if (left != NULL && right != NULL) return root;
    if (left == NULL) return right;
    return left;
    }
    };

    int main() {
    TreeNode *root = new TreeNode(3);
    root->left = new TreeNode(5);
    root->right = new TreeNode(1);
    root->left->left = new TreeNode(6);
    root->left->right = new TreeNode(2);
    root->right->left = new TreeNode(0);
    root->right->right = new TreeNode(8);
    root->left->right->left = new TreeNode(7);
    root->left->right->right = new TreeNode(4);
    TreeNode *p = root->left;
    TreeNode *q = root->right;
    TreeNode *k = root->left->right->right;

    Solution sol;
    TreeNode *res = sol.lowestCommonAncestor(root, p, q);
    cout << res->val << endl;
    res = sol.lowestCommonAncestor(root, p, k);
    cout << res->val << endl;
    return 0;
    

    }

    本文为博主原创文章,未经博主允许禁止转载,需转载请注明出处,欢迎指正!
  • 相关阅读:
    Polly
    ELK
    Python基础三(选择,循环)
    Python基础二(输入与输出)
    Python关键字
    Python基础一(基本类型和运算符)
    Python发展史
    在虚拟机(vmware)上安装CentOS
    centos7联网
    Hashmap的实现
  • 原文地址:https://www.cnblogs.com/caoer/p/15722448.html
Copyright © 2020-2023  润新知