博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 117 Populating Next Right Pointers in Each Node 2
阅读量:4659 次
发布时间:2019-06-09

本文共 2540 字,大约阅读时间需要 8 分钟。

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 和之前的有不一样的是 最左边的点不确定 然后每个节点的孩子不确定, 所以要判断的条件会多一些
class Solution {public:    void connect(TreeLinkNode *root) {        if (root == NULL)            return;        if (root->right == NULL && root ->left == NULL)            return;        TreeLinkNode * p = root;        TreeLinkNode * current = root;        int flag = 0;        while (root ->left || root ->right || root->next)        {            p = root;             flag = 0;            while(p!=NULL)            {                if(p->left == NULL && p->right == NULL)                {                    p = p -> next;                    continue;                 }                else                 {                    if (flag == 0)                    {                        current = p->left ? p->left:p->right;                         if (p->left != NULL && p->right != NULL)                        {                            current ->next = p ->right ;                            current = current ->next ;                         }                        flag = 1;                    }                    else                     {                        if(p->left && p->right)                        {                            current ->next = p->left;                             p->left ->next = p -> right;                            current = p->right;                         }                        else if(p->left!=NULL && p->right==NULL)                        {                            current ->next = p->left;                             current = p->left;                         }                        else                         {                            current ->next = p->right;                             current = p->right;                         }                    }                }                  p = p->next;            }            if (root -> left)                root = root ->left;             else if (root ->right)                root = root ->right;             else if(root ->next)                root = root ->next;         }    }};

 

 

转载于:https://www.cnblogs.com/zhuguanyu33/p/4557613.html

你可能感兴趣的文章
CodeForces 666A Reberland Linguistics(DP)
查看>>
【题解】洛谷P2421[NOI2002]荒岛野人 (Exgcd)
查看>>
EXT学习笔记——几个小问题
查看>>
《算法图解》——第二章 选择排序
查看>>
多Region下HBase写入问题
查看>>
VueJs2.0建议学习路线
查看>>
temporary
查看>>
idea创建maven-archetype-webapp项目无java目录
查看>>
WebSocket入门及示例
查看>>
第四周-第08章节-Python3.5-装饰器
查看>>
单点登录跳转失败(原因是 主票据申请子票据失败) asp.net 同站点下不同应用间不同版本Framework问题...
查看>>
1、搭建Struts2开发环境
查看>>
DAY1--Python基础1
查看>>
Use "OR" in SQL with caution
查看>>
[super class] 和 [self superclass] 区别
查看>>
Visual Studio 2012 無法開啟 ASP.NET MVC2 專案的解決流程筆記
查看>>
mapreduce中map和reduce个数
查看>>
3 [面向对象]-组合,抽象类,接口
查看>>
2-[HTML]--介绍
查看>>
C# lambda 实现 Ascii 排序
查看>>