• leetcode Populating Next Right Pointers in Each Node


    看图就知道想要做什么事了。

    Given a binary tree

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

    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
    初始所有人的next都是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).
    要求常数额外空间。
     
    一个递归就搞定了,就是递归让每一个节点他的左右子树通过next链接,直至到最后一层,然后递归左右节点,继续让他们的左右子树通过next链接。
    /**
     * 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:
    
    //每一层把left的right的next设置为right的left
    void lr2rl(TreeLinkNode *root)
    {
        if (!root) return;
        TreeLinkNode *lr = root -> left, *rl = root -> right;
        
        while(lr && rl)
        {
            lr -> next = rl;
            lr = lr -> right;
            rl = rl -> left;
        }
        lr2rl(root -> left);
        lr2rl(root -> right);
    }
        void connect(TreeLinkNode *root) 
        {
            lr2rl(root);
        }
    };

     也可以每层每层的完成:

    /**
     * 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 ;
            TreeLinkNode *lf = root;
            while (root)
            {
                if (root -> left)
                    root -> left -> next = root -> right;
                if (root -> next && root -> next -> left)
                    root -> right -> next = root -> next -> left;
                root = root -> next;
            }
            connect(lf -> left);
            connect(lf -> right);
        }
    };

     可以用非递归的方法,把每一行当做一个queue,每次从最左边开始处理到最右边。记录下一层的最左边。继续。直至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:
        //非递归法,将做好的每一行当做一个queue
        void connect(TreeLinkNode *root) 
        {
            if (!root) return ;
            TreeLinkNode *lf;
            while(root)
            {
                lf = root -> left;
                while(root)
                {
                    if (lf)
                        root -> left -> next = root -> right;
                    if (root -> next && root -> next -> left)
                        root -> right -> next = root -> next -> left;
                    root = root -> next;
                }
                root = lf;
            }
        }
    };
     
  • 相关阅读:
    去掉返回数据存在HTML节点问题
    ios8 地图不能定位问题的解决办法
    日期选择器
    定位的系统实现简单方法
    NSMutableString 的使用例子
    UIImagePickerController--------图片选取器
    代码中判断网络类型的类别
    Gitbook Android App
    Ionic 整合 pixi.js
    ionic app调试问题
  • 原文地址:https://www.cnblogs.com/higerzhang/p/4134025.html
Copyright © 2020-2023  润新知