2018-08-09 16:01:40

一、Populating Next Right Pointers in Each Node

问题描述:

问题求解:

由于是满二叉树,所以很好填充。

    public void connect(TreeLinkNode root) {
if (root != null) {
if (root.left != null) root.left.next = root.right;
if (root.right != null && root.next != null) root.right.next = root.next.left;
connect(root.left);
connect(root.right);
}
}

二、Populating Next Right Pointers in Each Node II

问题描述:

问题求解:

本题中因为不再是满二叉树,因此不能简单的使用先序遍历进行求解,因为先序遍历是有先后顺序的,因此单纯采用先序遍历会出现丢失的现象。因此只能使用按行求解。

    public void connect(TreeLinkNode root) {
if (root == null) return;
TreeLinkNode dummy = new TreeLinkNode(-1);
for (TreeLinkNode cur = root, pre = dummy; cur != null; cur = cur.next) {
if (cur.left != null) {
pre.next = cur.left;
pre = pre.next;
}
if (cur.right != null) {
pre.next = cur.right;
pre = pre.next;
}
}
connect(dummy.next);
}

最新文章

  1. javascript作用域链与原型链有联系吗?
  2. interface
  3. Linux初学---->WinSCP+Putty
  4. 设计模式之美:Iterator(迭代器)
  5. MVC模式的学生信息增删改查
  6. 信息图:iOS 7开发者需要知道的事
  7. poj3280Cheapest Palindrome(记忆化)
  8. 类(class)能不能自己继承自己(转)
  9. 单身福利来了:VR恋人为你量身定制一个女朋友
  10. 【Python@Thread】thread模块
  11. 牛腩新闻公布系统--学习Web的小技巧汇总
  12. VS2005 添加lib 的方法
  13. 设计模式六: 模板方法(Template Method)
  14. [CSS] 点击事件触发的动画
  15. composer lavarel 安装
  16. 在Java Web程序中使用监听器可以通过以下两种方法
  17. mysql.user表中Host为%的含义
  18. HTTP 的长连接和短连接
  19. LNMP 简介
  20. Java入门:练习——自定义通用工具类

热门文章

  1. vmware tool安装
  2. Fiddler过滤指定域名
  3. uva 13598
  4. Python:slice与indices
  5. 数据仓库基础(十二)Informatica组件(2)
  6. javascript 中function(){},new function(),new Function(),Function 摘录
  7. 火狐使用Ctrl新开窗口不生效
  8. 为什么采用4~20mA的电流来传输模拟量?(转)
  9. Python学习笔记之@classmethod与@staticmethod
  10. bzoj1654 / P2863 [USACO06JAN]牛的舞会The Cow Prom