在每个节点填充向右的指针 Populating Next Right Pointers in Each Node
2024-10-19 02:21:18
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);
}
最新文章
- javascript作用域链与原型链有联系吗?
- interface
- Linux初学---->;WinSCP+Putty
- 设计模式之美:Iterator(迭代器)
- MVC模式的学生信息增删改查
- 信息图:iOS 7开发者需要知道的事
- poj3280Cheapest Palindrome(记忆化)
- 类(class)能不能自己继承自己(转)
- 单身福利来了:VR恋人为你量身定制一个女朋友
- 【Python@Thread】thread模块
- 牛腩新闻公布系统--学习Web的小技巧汇总
- VS2005 添加lib 的方法
- 设计模式六: 模板方法(Template Method)
- [CSS] 点击事件触发的动画
- composer lavarel 安装
- 在Java Web程序中使用监听器可以通过以下两种方法
- mysql.user表中Host为%的含义
- HTTP 的长连接和短连接
- LNMP 简介
- Java入门:练习——自定义通用工具类
热门文章
- vmware tool安装
- Fiddler过滤指定域名
- uva 13598
- Python:slice与indices
- 数据仓库基础(十二)Informatica组件(2)
- javascript 中function(){},new function(),new Function(),Function 摘录
- 火狐使用Ctrl新开窗口不生效
- 为什么采用4~20mA的电流来传输模拟量?(转)
- Python学习笔记之@classmethod与@staticmethod
- bzoj1654 / P2863 [USACO06JAN]牛的舞会The Cow Prom