二叉树 层次遍历 (queue)遍历的变式题(leetcode)

利用队列结构和二叉树,变换求解

116. Populating Next Right Pointers in Each Node

题目地址
https://leetcode.com/problems/populating-next-right-pointers-in-each-node/

117. Populating Next Right Pointers in Each Node II

题目地址
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

与 116 题一致

ac代码如下

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {
        if (root == NULL)
            return;

        queue que;
        queue<int> queC;

        que.push(root);
        queC.push(1);

        while (!que.empty())
        {
            TreeLinkNode* top = que.front();
            que.pop();
            int ceng = queC.front();
            queC.pop();

            if (que.empty())
            {
                top->next = NULL;
            }
            else if (ceng == queC.front())
            {
                top->next = que.front();
            }
            else{
                top->next = NULL;
            }

            if (top->left != NULL)
            {
                que.push(top->left);
                queC.push(ceng + 1);
            }

            if (top->right != NULL)
            {
                que.push(top->right);
                queC.push(ceng + 1);
            }
        }// end while

    }
};

199. Binary Tree Right Side View

题目地址
https://leetcode.com/problems/binary-tree-right-side-view/

题目描述
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

For example:
Given the following binary tree,

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---

You should return [1, 3, 4].

ac代码如下

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    int Height(TreeNode* root){
        if (root == NULL)
            return 0;
        else if (root->left == NULL && root->right == NULL)
            return 1;
        else{
            return 1 + max(Height(root->left), Height(root->right));
        }
    }

    vector<int> rightSideView(TreeNode* root) {
        vector<int> ans;
        if (root == NULL)
            return ans;

        int h = Height(root);


        queue que;
        queue<int> queCeng;
        que.push(root);
        queCeng.push(1);

        int last = 0;
        while (!que.empty())
        {
            TreeNode* top = que.front();
            que.pop();
            int ceng = queCeng.front();
            queCeng.pop();

            if (ceng != last)
            {
                ans.push_back(top->val);
                if (ceng == h)
                    break;
                last = ceng;
            }
            if (top->right != NULL)
            {
                que.push(top->right);
                queCeng.push(ceng + 1);
            }
            if (top->left != NULL)
            {
                que.push(top->left);
                queCeng.push(ceng + 1);
            }
        }

        return ans;
    }
};

你可能感兴趣的:(#,各种二叉树)