leetcode — populating-next-right-pointers-in-each-node
/**
*
* Source : https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/
* Source : https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
*
*
*
* Given a binary tree
*
* struct TreeLinkNode {
* TreeLinkNode *left;
* TreeLinkNode *right;
* TreeLinkNode *next;
* }
*
* Populate each next pointer to point to its next right node.
* If there is no next right node, the next pointer should be set to NULL.
*
* Initially, all next pointers are set to NULL.
*
* Note:
*
* You may only use constant extra space.
* You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
*
* For example,
* Given the following perfect binary tree,
*
* 1
* / \
* 2 3
* / \ / \
* 4 5 6 7
*
* After calling your function, the tree should look like:
*
* 1 -> NULL
* / \
* 2 -> 3 -> NULL
* / \ / \
* 4->5->6->7 -> NULL
*
* 下面是II,如果是一般二叉树而不是特殊的完全二叉树,求解,因为适合II的解同样可以解决问题I,这里直接解决问题II
*
*
* * 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
*
*/
public class PopulatingNextRightPointers {
/**
*
* 将二叉树的每一层节点连接成为一个单向链表
*
* 假设当前层已经连接好,那么下一层怎么连接?
* 从当前层的最左节点开始
* 如果当前根节点root的左右子树都为空,则直接向后移动一个节点
*
* leftMost = root.left != null ? root.left : root.right
* cur = leftMost
*
* 循环当前一层的所有节点,知道2下一个节点为空,说明当前层循环完
* 左右子树中至少有一个,:
* 如果cur==左子树,如果右子树不为空则将cur.next = root.right,且将cur和root向后移,cur = cur.next,root = root.next
* 如果cur == 右子树,直接将根节点向后移root = root.next
* 如果cur和左右子树都不相同,则说明cur属于上一个根节点的子节点
* 如果当前根节点的左右子节点都为空,根节点向后移
* 否则更新cur,cur = root.left != null ? root.left : root.right,cur = cur.next
*
* 对下一层(上面已经连接成链表)进行递归
*
* @param root
* @return
*/
public TreeLinkedNode populate (TreeLinkedNode root) {
// 没有子节点,将当前层向后移动一个
while (root != null && root.leftChild == null && root.rightChild == null) {
root = root.next;
}
if (root == null) {
return null;
}
TreeLinkedNode leftMost = root.leftChild != null ? root.leftChild : root.rightChild;
TreeLinkedNode cur = leftMost;
while (root != null) {
if (cur == root.leftChild) {
if (root.rightChild != null) {
cur.next = root.rightChild;
cur = cur.next;
}
root = root.next;
} else if (cur == root.rightChild) {
root = root.next;
} else {
if (root.leftChild == null && root.rightChild == null) {
root = root.next;
} else {
cur.next = root.leftChild != null ? root.leftChild : root.rightChild;
cur = cur.next;
}
}
}
populate(leftMost);
return root;
}
public TreeLinkedNode populateByIterator (TreeLinkedNode root) {
TreeLinkedNode leftMost = root;
while (leftMost != null) {
root = leftMost;
// 没有子节点,将当前层向后移动一个
while (root != null && root.leftChild == null && root.rightChild == null) {
root = root.next;
}
if (root == null) {
return null;
}
leftMost = root.leftChild != null ? root.leftChild : root.rightChild;
TreeLinkedNode cur = leftMost;
while (root != null) {
if (cur == root.leftChild) {
if (root.rightChild != null) {
cur.next = root.rightChild;
cur = cur.next;
}
root = root.next;
} else if (cur == root.rightChild) {
root = root.next;
} else {
if (root.leftChild == null && root.rightChild == null) {
root = root.next;
} else {
cur.next = root.leftChild != null ? root.leftChild : root.rightChild;
cur = cur.next;
}
}
}
}
return root;
}
private class TreeLinkedNode {
int value;
TreeLinkedNode next;
TreeLinkedNode leftChild;
TreeLinkedNode rightChild;
public TreeLinkedNode(int value) {
this.value = value;
}
}
public TreeLinkedNode createTree (char[] treeArr) {
TreeLinkedNode[] tree = new TreeLinkedNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeLinkedNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
}
public static void main(String[] args) {
PopulatingNextRightPointers nextRightPointers = new PopulatingNextRightPointers();
char[] arr = new char[]{'1','2','3','4','5','#','7'};
TreeLinkedNode root = nextRightPointers.createTree(arr);
nextRightPointers.populate(root);
root = nextRightPointers.createTree(arr);
nextRightPointers.populateByIterator(root);
}
}
leetcode — populating-next-right-pointers-in-each-node的更多相关文章
- LeetCode:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- [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 ...
- [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- 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 ...
- [leetcode]Populating Next Right Pointers in Each Node II @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
- LeetCode: Populating Next Right Pointers in Each Node II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- LEETCODE —— Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- LeetCode - Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- LeetCode: Populating Next Right Pointers in Each Node 解题报告
Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode { Tree ...
- [LeetCode] [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 ...
随机推荐
- 黑洞版视频裂变程序【接口版】全新上线,全新UI,支持分享数据统计
黑洞版视频裂变程序[接口版]全新上线,全新UI,支持分享数据统计! 后台效果 程序统一售价:1899/套(包安装,包更新) 注:本程序不属于之前视频程序的更新版,展现形式和广告位设置均不同,是 ...
- 20181117-python第二章学习小结-part2
浮点型补充: 有限小数与无限循环小数,不包括无理数! 小数点后面的数据运算太复杂,精确度不及整数! 尽量使用科学计数表示小数 列表学习(语法) 创建:[] list = [] #创建空表 list ...
- Hibernate HQL ③
迫切左外连接: - LEFT JOIN FETCH 关键字表示迫切左外连接检索策略 - list()方法返回的集合中存放实体对象的引用,每个 Department 对象关联的 Employee 集合都 ...
- QT—QTextEdit控件显示日志
功能:利用QTextEdit开发一个日志显示窗口.没有太多操作,需要实现的是日志自动向上滚动,总体的日志量可以控制在x行(比如300行)以内:其他的应用功能我后面继续添加 #include <Q ...
- 190327 Python登录接口
#!Author:John # _*_ coding: utf-8 _*_ #编写登录接口 #输入用户名密码 #认证成功后显示欢迎信息 #输错三次后锁定 import sys, os, getpass ...
- 马昕璐 201771010118《面向对象程序设计(java)》第十六周学习总结
第一部分:理论知识学习部分 程序:一段静态的代码,应用程序执行的蓝本. 进程:是程序的一次动态执行,它对应了从代码加载.执行至执行完毕的一个完整过程. 多线程:进程执行过程中产生的多条执行线索,比进程 ...
- echarts、higncharts折线图或柱状图显示数据为0的点
echarts.higncharts折线图或柱状图只需要后端传到前端一段json数据,接送数据的x轴与y周有对应数据,折线图或柱状图就会渲染出这数据. 比如,x轴表示美每天日期,y轴表示数量.他们的数 ...
- Java和js操作json
Js中 Json字符串转json对象 //将json格式的字符串转为json对象 var t = JSON.parse('{"name":123}'); alert(t.name) ...
- Java内存模型锦集
[内存操作与内存屏障] 内存模型操作: lock(锁定) : 作用与主内存的变量, 它把一个变量标识为一条线程独占的状态 unlock(解锁) : 作用于主内存变量, 它把一个处于锁定状态的变量释放出 ...
- 去掉input在type="number"时右边的上下箭头
加了代码之后: input::-webkit-outer-spin-button, input::-webkit-inner-spin-button{ -webkit-appearance: none ...