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

这道题延续Populating Next Right Pointers in Each Node 的思路,使用层搜和上一层的linkedlist连接实现O(1)的空间复杂度,但是实际实现时因为不是完全二叉树,不能直接通过上一层建立下一层的连接(时间复杂度高)。而需要同时保存下一层的prev已方便连接。实现时需要判断当前上层结点是否有左右子树,代码如下:

class Solution(object):def connect(self, root):""":type root: TreeLinkNode:rtype: nothing"""if not root:returnhead = root    #low level's start nodeprev = None    #low level's previous nodeupCur = None   # higher level's currrent nodewhile(head):upCur = head prev = Nonehead = Nonewhile upCur:if upCur.left:if prev == None:prev = upCur.lefthead = prevelse:prev.next = upCur.leftprev = prev.next if upCur.right:if prev == None:prev = upCur.righthead = prevelse:prev.next = upCur.rightprev = prev.nextupCur = upCur.next

另外一种需要判断的代码如下:

class Solution(object):def connect(self, root):""":type root: TreeLinkNode:rtype: nothing"""if not root:returnleftMost = root               #higher level point while leftMost:prev = leftMostwhile prev and (not prev.left) and (not prev.right):  #no low level nodes should be connectedprev = prev.nextif not prev:              #the lowest level of treereturn leftMost = prev.left if prev.left else prev.right  #for the next loop, leftMost is not Nonecur = leftMostwhile(prev):              #higer level's node if cur == prev.left:if prev.right:    #if root.right is not None, find the child of other nodescur.next = prev.rightcur = cur.nextprev = prev.nextelif cur == prev.right:  prev = prev.next  #can't connected directedly.else:if (not prev.left) and (not prev.right):prev = prev.nextcontinue      #can't find cur.next through this rootcur.next = prev.left if prev.left else prev.rightcur = cur.next

这种解法分情况讨论目前遍历到的下一层结点和目前当前层结的关系,讨论比较复杂。不如上面那种解法方便。

转载于:https://www.cnblogs.com/sherylwang/p/5483685.html

Populating Next Right Pointers in Each Node II相关推荐

  1. 【To Understand!】LeetCode 117. Populating Next Right Pointers in Each Node II

    LeetCode 117. Populating Next Right Pointers in Each Node II Solution1:我的答案 层次遍历 /*** Definition for ...

  2. 117 Populating Next Right Pointers in Each Node II

    117 Populating Next Right Pointers in Each Node II 就是 Bibary Tree Level order Traverse class Solutio ...

  3. Leetcode: Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree ...

  4. leetcode - Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  5. LeetCode 117. Populating Next Right Pointers in Each Node II

    原题链接在这里:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题目: Given a bi ...

  6. 117. Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  7. LeetCode OJ - Populating Next Right Pointers in Each Node II

    题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...

  8. [Leetcode][JAVA] Populating Next Right Pointers in Each Node II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  9. 117. Populating Next Right Pointers in Each Node II 计算右边的附属节点

    [抄题]: Given a binary tree struct TreeLinkNode {TreeLinkNode *left;TreeLinkNode *right;TreeLinkNode * ...

最新文章

  1. ios 拍照上传到服务器_ios端浏览器拍照上传到服务器,图片被旋转90度 php 解决方案...
  2. Python~win32com~Excel
  3. java框架概念_java概念(2)
  4. python地理_【Python教程】地理可视化
  5. Python3.6 的字符串内建函数
  6. 基于Taro开发小程序笔记--04路由跳转的几种方式
  7. 网课时代,在线教育的新机遇在哪?
  8. Java8 stream流分页(手动分页)
  9. struts1、 struts2所有版本jar包下载地址大全
  10. ceb怎么转换成word_关于把CEB文件转换成word文档!!!
  11. 【智能算法】PSO粒子群算法求解无约束多元函数最值(Java代码实现)
  12. Java毕设项目银行贷款管理系统计算机(附源码+系统+数据库+LW)
  13. 配置 Exchange ActiveSync 身份验证
  14. 弗洛伊德的兔子与乌龟
  15. 易语言-post登陆百度
  16. 推断车祸事件论文matlab,预测模型论文,关于基于优化GM(1,1)模型的水上交通事故预测相关参考文献资料-免费论文范文...
  17. 前端实现“查看更多”效果
  18. element ui 前台模板_一个干净优雅的 Element UI Admin 模板
  19. 忘记阿里云用户密码,如何修改
  20. python的curses模块使用

热门文章

  1. “诺奖风向标”2021拉斯克奖公布:授予mRNA疫苗、光遗传学以及戴维·巴尔的摩...
  2. IMT-2030(6G)推进组发布《6G总体愿景与潜在关键技术》白皮书
  3. 贝叶斯网络之父Judea Pearl力荐、LeCun点赞,这篇长论文全面解读机器学习中的因果关系...
  4. 北欧小国的宏大AI实验: 让1%的人口接受人工智能培训
  5. 未来的设计:我们正在进入“计算设计”时代
  6. MIT开发出新界面系统 操作员可用思维控制机器人
  7. 太真实!当程序员接手了新项目...... | 每日趣闻
  8. 漫话:如何给女朋友解释为什么 Java 中1000==1000为 false,而100==100为 true?
  9. 如何一眼识别蚂蚁集团身价千万的阿里人
  10. 2013年蓝桥杯省赛C/C++A组真题解析