/**
*
* 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的更多相关文章

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

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

  3. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

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

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

  6. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

    Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...

  7. LEETCODE —— Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

  8. LeetCode - Populating Next Right Pointers in Each Node II

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

  9. LeetCode: Populating Next Right Pointers in Each Node 解题报告

    Populating Next Right Pointers in Each Node TotalGiven a binary tree struct TreeLinkNode {      Tree ...

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

随机推荐

  1. Bphero-UWB 基站0 和 电脑串口数据格式定义

    基站0 通过串口将系统中测得的距离信息发送到电脑,电脑定位软件通过三边定位算法计算出TAG的坐标,基站0 和 定位软件之间的数据格式定义如下(对官方数据结构进行了简化) 更多UWB定位信息请参阅论坛b ...

  2. IntelliJ IDEA最新破解版2018.3.1(附2018.2.2 完美破解教程)

    2018.3.1最新版破解 1.官网下载IDEA 2018.3.1的商业版本点我去下载 2.破解jar下载:JetbrainsIdesCrack-3.4-release-enc.jar点我去下载 3. ...

  3. 在deepin上安装YouCompleteMe

    详细安装步骤在github上有,https://github.com/Valloric/YouCompleteMe,我这里是自己总结的简化版安装步骤. 步骤1.安装Vundle 首先,clone到本地 ...

  4. react.JS基础

    1.ReactDOM.render() React.render 是 React 的最基本方法,用于将模板转为 HTML 语言,并插入指定的 DOM 节点. <!DOCTYPE html> ...

  5. Java 将容器 Map中的内容保存到数组

    import java.util.Map; import java.util.HashMap; import java.util.Map.Entry; public class mapToArr { ...

  6. 【尺取法】Jurisdiction Disenchantment

    [尺取法]Jurisdiction Disenchantment PROBLEM 时间限制: 1 Sec 内存限制: 128 MB 题目描述 The Super League of Paragons ...

  7. 4.28Linux(6)

    2019-4-28 21:27:41 明天回家.回家继续学Linux还好有个服务器!!!感觉有个属于自己的服务器感觉好爽啊!! 越努力越幸运!永远不要高估自己!!! Nginx安装 服务器的请求原理 ...

  8. Java for Android 第三周学习总结

    第五章 核心类 java.lang.Object中的方法: clone(创建并返回该对象的一个副本.实现这个方法的一个类,将支持对象的复制) equals(将该对象和传入的对象进行比较.必须实现这个算 ...

  9. Jvm 内存模型 —— GC

    一.Jvm 原理 二.Jvm 运行时数据区( Run-Time Data Areas ) (主要是关于 non-stack 区域的详细划分) 从上图可以清楚地看到:程序计数器.Jvm 栈.本地方法栈 ...

  10. 30、vue 过滤器(filters)

    filter Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化.过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持).过滤器应该被添加在 Ja ...