• LeetCode: Populating Next Right Pointers in Each Node I && II


    Title: 

    Given a binary tree

        struct TreeLinkNode {
          TreeLinkNode *left;
          TreeLinkNode *right;
          TreeLinkNode *next;
        }
    

    Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

    Initially, all next pointers are set to NULL.

    Note:

    • You may only use constant extra space.
    • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

    For example,
    Given the following perfect binary tree,

             1
           /  
          2    3
         /   / 
        4  5  6  7
    

    After calling your function, the tree should look like:

             1 -> NULL
           /  
          2 -> 3 -> NULL
         /   / 
        4->5->6->7 -> NULL
    思路:使用递归
    /**
     * 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) {
            maker(root,NULL);
        }
        
        void maker (TreeLinkNode* first, TreeLinkNode* second){
            if (!first)
                return ;
            first->next = second;
            maker(first->left,first->right);
            if (second){
                maker(first->right,second->left);
                maker(second->left,second->right);
            }
        }
    };

    非递归就是BFS搜索

    /**
     * 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)
                return ;
            queue<TreeLinkNode*> Q;
            int pre_count = 1;
            int cur_count = 0;
            Q.push(root);
            while (!Q.empty()){
                TreeLinkNode* p = Q.front();//关键
                Q.pop();
                pre_count--;
                if (!Q.empty() && pre_count != 0){
                    p->next = Q.front();
                }
                
                if (p->left){
                    Q.push(p->left);
                    cur_count++;
                }
                if (p->right){
                    Q.push(p->right);
                    cur_count++;
                }
                if (pre_count == 0){
                    pre_count = cur_count;
                    cur_count = 0;
                }
            }
        }
    };

    Title

    Follow up for problem "Populating Next Right Pointers in Each Node".

    What if the given tree could be any binary tree? Would your previous solution still work?

    Note:

    • You may only use constant extra space.

    For example,
    Given the following binary tree,

             1
           /  
          2    3
         /     
        4   5    7
    

    After calling your function, the tree should look like:

             1 -> NULL
           /  
          2 -> 3 -> NULL
         /     
        4-> 5 -> 7 -> NULL

    使用上面的非递归
  • 相关阅读:
    ps眼睫毛笔刷的使用
    七款使用命令行的PNG图像处理工具
    有关滤镜的相关知识
    分析不同滤镜的作用
    总结网页的制作概括的几个部分
    网站内容和外链在优化中的占比
    让IE支持HTML5
    认识Linux的磁盘配额(转载)
    Samba高级服务器配置 (转载)
    CentOS5启用Telnet服务详解(转载)
  • 原文地址:https://www.cnblogs.com/yxzfscg/p/4505416.html
Copyright © 2020-2023  润新知