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

我以前为啥要做那么复杂呢

ref http://www.cnblogs.com/springfor/p/3889327.html

关键在于找到有效的下家P

 public void connect(TreeLinkNode root) {  if (root == null) return;  TreeLinkNode p = root.next;  while(p!=null){if(p.left!=null){p = p.left;break;}if(p.right!=null){p = p.right;break;}p = p.next;}if(root.right!=null)root.right.next = p;if(root.left!=null){if(root.right!=null){root.left.next = root.right;}elseroot.left.next =p;}connect(root.right);connect(root.left);}

转载于:https://www.cnblogs.com/jiajiaxingxing/p/4565358.html

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

  1. Populating Next Right Pointers in Each Node II

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

  2. 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 ...

  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 tre ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. [Leetcode Week15]Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...

  8. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  9. [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 ...

最新文章

  1. 隐藏画质代码_如何让千元机高画质满帧吃鸡?最全教程奉上!
  2. python读取excel日期内容读出来是数字-Python xlrd读取excel日期类型的2种方法
  3. RAMdisk保存文件方案,解决关机后无盘镜像文件没了的方案
  4. 深大教授开源的人脸检测库,速度号称史上最快
  5. 等于x分之a的平方的导数_清华学霸丨手把手教你导数大题如何骗分(文理通用),家长为孩子收...
  6. html鼠标悬停效果_【开发小技巧】023—如何使用HTML和CSS实现3D文字效果
  7. 视觉SLAM十四讲学习笔记---前三讲学习笔记总结之SLAM的作用、变换和位姿表示
  8. 【华为云技术分享】玩转云上数据湖,解析Serverless 技术落地
  9. 电子信息计算机调剂到能源动力,江苏科技大学机械、电子信息、电子信息能源动力、土木水利2020年考研调剂信息...
  10. 西门子1500和300哪个贵_西门子PLC S7-300和1500之间的通信资源管理之比较?
  11. PS光效插件 DFT 1.1.1.3汉化破解版,比灯光工厂更强大支持PSCC2019
  12. Reeder for Chrome:让 Google Reader 更加简洁美观
  13. NTC热敏电阻温度计算方法,Steinhart-Hart方程和B值法
  14. 老人为戒烟嗑瓜子 脚趾腐烂散发难闻臭味令孙儿恶心至极
  15. C# 读写xml、excel、word、ppt、access
  16. wpf label下划线不显示的问题
  17. 服务器将系统盘,如何将Windows服务器系统的inetpub目录移到其它盘
  18. STM32编程环境配置(kile5)
  19. Android 按钮实现按压水波纹效果
  20. javaWeb核心技术第六篇之BootStrap

热门文章

  1. java calendar 时分秒_Java中Calendar类的常用方法(对时间进行计算的类)
  2. java随机数_Java随机
  3. 排序算法python实现_合并排序算法– Java,C和Python实现
  4. C# 窗体半透明,控件不透明
  5. AVCaptureDevice 属性介绍
  6. 工作总结 2017年8月11日 转换为datatime
  7. JVM之静态编译优化以及JIT编译
  8. HTML(八)------ 布局
  9. Lodash chain功能(Lazy Evaluation)介绍
  10. C基础(36——40)