当前位置:网站首页>420 sequence traversal of binary tree 2 (429. sequence traversal of n-ary tree, 515. find the maximum value in each tree row, 116. fill in the next right node pointer of each node, 104. maximum depth

420 sequence traversal of binary tree 2 (429. sequence traversal of n-ary tree, 515. find the maximum value in each tree row, 116. fill in the next right node pointer of each node, 104. maximum depth

2022-06-25 08:10:00 liufeng2023

429. N Sequence traversal of the fork tree

 Insert picture description here

class Solution {
    
public:
    vector<vector<int>> levelOrder(Node* root) {
    
        vector<vector<int>> res;
        queue<Node*> que;

        if (root != nullptr) que.push(root);

        while (!que.empty())
        {
    
            vector<int> temp;
            int size = que.size();

            for (int i = 0; i < size; i++)
            {
    
                Node* node = que.front();
                que.pop();

                temp.push_back(node->val);

                for (int i = 0; i < (node->children.size()); i++)
                {
    
                    if (node->children[i]) que.push(node->children[i]);
                }
            }
            res.push_back(temp);
        }
        return res;
    }
};

 Insert picture description here

515. Find the maximum in each tree row

 Insert picture description here

class Solution {
    
public:
    vector<int> largestValues(TreeNode* root) {
    
        vector<int> res;
        queue<TreeNode*> que;

        if (root != nullptr) que.push(root);

        while (!que.empty())
        {
    
            int size = que.size();
            int temp = INT32_MIN;

            for (int i = 0; i < size; i++)
            {
    
                TreeNode* node = que.front();
                que.pop();

                temp = (node->val > temp) ? node->val : temp;
                if (node->left)  que.push(node->left);
                if (node->right) que.push(node->right);
            }
            res.push_back(temp);
        }
        return res;
    }
};

 Insert picture description here

116. Fill in the next right node pointer for each node

 Insert picture description here

class Solution {
    
public:
    Node* connect(Node* root) {
    
        queue<Node*> que;
        if (root != NULL) que.push(root);
        while (!que.empty()) {
    
            int size = que.size();
            // vector<int> vec;
            Node* nodePre;
            Node* node;
            for (int i = 0; i < size; i++) {
    
                if (i == 0) {
    
                    nodePre = que.front(); //  Take out the head node of the first layer 
                    que.pop();
                    node = nodePre;
                } else {
    
                    node = que.front();
                    que.pop();
                    nodePre->next = node; //  Previous node of this layer next Point to this node 
                    nodePre = nodePre->next;
                }
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
            nodePre->next = NULL; //  The last node of this layer points to NULL
        }
        return root;

    }
};

 Insert picture description here

104. The maximum depth of a binary tree

 Insert picture description here

class Solution {
    
public:
    int maxDepth(TreeNode* root) {
    
        queue<TreeNode*> que;
        int res = 0;
        if (root)    que.push(root);

        while (!que.empty())
        {
    
            int size = que.size();

            for (int i = 0; i < size; i++)
            {
    
                TreeNode* node = que.front();
                que.pop();

                if (node->left)  que.push(node->left);
                if (node->right)  que.push(node->right);
            }
            res++;
        }
        return res;
    }
};

 Insert picture description here

111. Minimum depth of binary tree

 Insert picture description here

class Solution {
    
public:
    int minDepth(TreeNode* root) {
    
        queue<TreeNode*> que;
        int res = 0;

        if (root)    que.push(root);

        while (!que.empty())
        {
    
            int size = que.size();
            res++;
            for (int i = 0; i < size; i++)
            {
    
                TreeNode* node = que.front();
                que.pop();

                if (node->left == nullptr && node->right == nullptr)   return res;
                if (node->left)  que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return res;
    }
};

 Insert picture description here

原网站

版权声明
本文为[liufeng2023]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250603310547.html