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
[分析] 这题和它的基础版本主体思路一致,BFS, 难点在于叶子节点可能出现在任何一层,在为某个节点赋予next指针时需要费工夫需找一番,按这个思路实现了Method1, 花费了近两小时才调对。翻看了自己的历史记录,看到Method2,代码精简一半,目测是网上找的答案~
yb君指出两个实现思路是一致的,Method1不断优化就能得到Method2, 漂亮的代码不是一蹴而就的,需要优化迭代~
public class Solution {
// Method 2
public void connect(TreeLinkNode root) {
while(root != null){
TreeLinkNode nextLevel = null;//first node of the next level
TreeLinkNode prevNode = null; //previous node in the same level
while(root != null){
if(nextLevel == null)
nextLevel = root.left == null ? root.right : root.left;
if(root.left != null){
if(prevNode != null)
prevNode.next = root.left;
prevNode = root.left;
}
if(root.right != null){
if(prevNode != null)
prevNode.next = root.right;
prevNode = root.right;
}
root = root.next;
}
root = nextLevel;
}
}
//Method 1
public void connect(TreeLinkNode root) {
if (root == null)
return;
root.next = null;
TreeLinkNode curr = root, next = curr.next;
TreeLinkNode startNodeOfNextLevel = curr.left != null ? curr.left : curr.right;
while (curr != null && startNodeOfNextLevel != null) {
// skip leaf node in the current level
if (curr.left == null && curr.right == null) {
if (next != null) {
curr = next;
} else {
curr = startNodeOfNextLevel;
startNodeOfNextLevel = getStartNodeOfNextLevel(curr);
if (startNodeOfNextLevel == null)
break;
}
next = curr.next;
continue;
}
// search the next available node in the next level
TreeLinkNode nextCand = null;
while (next != null && nextCand == null) {
if (next.left != null)
nextCand = next.left;
else if (next.right != null)
nextCand = next.right;
else
next = next.next;
}
if(curr.left != null) {
curr.left.next = (curr.right != null) ? curr.right : nextCand;
}
if (curr.right != null) {
curr.right.next = nextCand;
}
if (nextCand == null) { // curr is the last non leaf node of the current level
curr = startNodeOfNextLevel;
startNodeOfNextLevel = getStartNodeOfNextLevel(curr);
if (startNodeOfNextLevel == null)
break;
} else { // curr is not the last node
curr = next;
}
next = curr.next;
}
}
private TreeLinkNode getStartNodeOfNextLevel(TreeLinkNode p) {
TreeLinkNode startNodeOfNextLevel = null;
while (p != null) {
if (p.left != null) {
startNodeOfNextLevel = p.left;
break;
} else if (p.right != null) {
startNodeOfNextLevel = p.right;
break;
} else {
p = p.next;
}
}
return startNodeOfNextLevel;
}
}
分享到:
相关推荐
java基础 java_leetcode题解之Populating Next Right Pointers in Each Node
java java_leetcode题解之Populating Next Right Pointers in Each Node.java
python python_leetcode题解116_Populating_Next_Right_Pointers_in_Each_Node
javascript js_leetcode题解之116-populating-next-right-pointers-in-each-node.js
Populating Next Right Pointers in Each Node I && II) PIE (未录入) CC150 (未录入) EPI (未录入) 每一个题库对应problems路径下的一个文件夹,每一个题目对应相应题库下的一个Python文件。每一个题目都要至少实现...
leetcode卡 leetcode_python ...Populating Next Right Pointers in Each Node 2020-01-29 783 Minimum Distance between bst nodes 2020-01-30 173 二叉搜索树迭代器 2020-01-31 230 二叉搜索树中
[117]填充每个节点的下一个右侧节点指针 II|populating-next-right-pointers-in-each-node-ii给定一个二叉树填充
力扣第七题通常是指“重建二叉树”(重建二叉树的英文原题可能是“Populating Next Right Pointers in Each Node”)。这是一道中等难度的题目,主要考察的是树结构和深度优先搜索(DFS)或广度优先搜索(BFS)的...
- **Populating Next Right Pointers in Each Node**:连接二叉树每个节点的下一个右侧节点。 - **Convert Sorted List/Array to Binary Search Tree**:将有序列表或数组转换为二叉搜索树。 - **Path Sum II**:...
lru cache leetcode leetcode 记录自己刷leetcode时遇到的一些值得记下来的题目, 分为一些子项 bytedance ...populating-next-right-pointers-in-each-node sum-root-to-leaf-numbers best-time-to-buy
四平方和定理 leetcode Leetcode practice Table of content Tree 92.reverse-linked-list-ii (反转链表 II) 94.binary-tree-in...116.populating-next-right-pointers-in-each-node (填充每个节点的下一个右侧节点
蓄水池算法 leetcode leetcode Post: 《双指针的魅力》 《常见面试题思想方法整理》 ...populating-next-right-pointers-in-each-node-ii: 二级指针代码虽然简洁优雅,但是对性能有影响,不如一级指针加if else判断快。