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 ...
随机推荐
- win10系统盘分多大合适?
WIN10系统盘分多大合适,想必许多网友在装系统的时候都犹豫不觉吧,不过现在的硬盘基本上都是512G 1T的机械硬盘,固态硬盘基本都是128G以上,256G几乎成为标配,所以WIN10系统盘空间还是足 ...
- user-agent | what is the "user-agent" ?
User Agent(用户代理) UA是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本.CPU 类型.浏览器及版本.浏览器渲染引擎.浏览器语言.浏览器插件等 通过抓包可以得到 下面是几个 ...
- 按模板批量修改Excel文件内容
Sub 按模板修改Excel文件() Dim MoBanWorkBook As Workbook Set MoBanWorkBook = Application.ActiveWorkbook Dim ...
- javascript函数传值问题(传值?址)
通常对于我们开发者来说,有不少人是忽略了这些小问题的,但是我们又必要去了解.因为今天一个朋友问起,所以写到这里来了, 在C#中,我们知道如果要往一个函数中传递参数的类型为对象,数组或者其他引用类型时. ...
- 关于外网无法访问阿里云主机CentOs
前两天阿里云ECS搞活动,所有买了个三年的Ecs,然后照着之前在虚拟机同样的搭建服务器,一切都很正常,可是 当我配置好防火墙和nginx之后,发现个问题,外网无法访问. 思考: 1.我的nginx没配 ...
- js将一篇文章中多个连续的<br>标签替换成两个连续的<br>标签
写本文的目的是今天恰好有一个之前做SEO的同事问我怎样把一篇文章中多个连续的br标签替换成两个连续的br标签,这里就牵涉到SEO层面的问题了. 在做SEO优化的时候,其中有一个需要注意的地方就是尽量减 ...
- h5唤起APP并检查是否成功
// 检查app是否打开 function checkOpen(cb) { const clickTime = +(new Date()); function check(elsTime) { if ...
- JUnit介绍(转)
测试的重要性毋庸再说,但如何使测试更加准确和全面,并且独立于项目之外并且避免硬编码,JUnit给了我们一个很好的解决方案.一.引子 首先假设有一个项目类SimpleObject如下: pu ...
- dedecms 后台可以上传mp4,但无法选择
原文链接 找到 /include/dialog/select_media.php 找到rmvb,在其后面加 “|mp4” 即可. 1
- Red and Black---POJ - 1979
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...