zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    };
     
  • 相关阅读:
    centos yum 安装php7.2
    Linux CentOS完全卸载PHP
    Linux: cp 复制文件、文件夹到文件夹
    CentOS 7 yum安装LAMP,LNMP并搭建WordPress个人博客网站
    cin循环输入控制问题
    有序数组中的二分查找
    二叉查找树中元素的删除操作
    如何生成能在没有安装opencv库及vs2010环境的电脑上运行的exe文件
    冒泡排序算法,选择排序算法,插入排序算法
    使用迭代法穷举1到N位最大的数
  • 原文地址:https://www.cnblogs.com/higerzhang/p/4134025.html
Copyright © 2011-2022 走看看