Question :
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 toNULL
.
Initially, all next pointers are set toNULL
.
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
Anwser 1: Travesal
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(NULL == root){
return;
}
TreeLinkNode *left = root;
while(left && left->left && left->right)
{
root = left;
while(root)
{
root->left->next = root->right; // connect two nodes of root
if(root->next){
root->right->next = root->next->left; // connect two isolated nodes
}
root=root->next; // traversal the same level line
}
left=left->left;
}
}
};
Anwser 2: Recursive
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(NULL == root){
return;
}
if(root->left){
root->left->next = root->right;
}
if(root->right){
root->right->next = root->next ? root->next->left : NULL;
}
connect(root->left);
connect(root->right);
}
};
Anwser 3: Queue
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(NULL == root){
return;
}
queue<TreeLinkNode *> Q;
if(root != NULL){
Q.push(root);
}
int row = 1;
int count = 0;
while(!Q.empty()){
TreeLinkNode *tmp = Q.front();
Q.pop();
count++;
if(tmp->left) Q.push(tmp->left);
if(tmp->right) Q.push(tmp->right);
if(count == row){
tmp->next = NULL;
count = 0;
row *= 2;
} else {
tmp->next = Q.front();
}
}
}
};
注意点:
1) Queue > Traversal > Recursive
2) Queue 没有递归的层次限制,可以使用很大的二叉树
分享到:
相关推荐
python python_leetcode题解116_Populating_Next_Right_Pointers_in_Each_Node
javascript js_leetcode题解之116-populating-next-right-pointers-in-each-node.js
leetcode卡 leetcode_python 项目介绍 想学学python,刷刷leetcode 打卡轨迹 2020-01-13 70 爬楼梯 2020-01-14 120 Triangle 2020-01-15 213 House Robberll -变种 198 337 2020-01-16 139 单词拆分 2020-01-20 104 ...
leetcode题库 pyshua Python 算法题练习 用法: python Judge.py library problem 例子: python Judge.py leetcode TwoSum 如何贡献: 收录题库 LeetCode (还有4题未录入, 分别为 LRU Cache, Copy List with Random ...
- **Populating Next Right Pointers in Each Node**:连接二叉树每个节点的下一个右侧节点。 - **Convert Sorted List/Array to Binary Search Tree**:将有序列表或数组转换为二叉搜索树。 - **Path Sum II**:...
[117]填充每个节点的下一个右侧节点指针 II|populating-next-right-pointers-in-each-node-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 Post: 《双指针的魅力》 《常见面试题思想方法整理》 ...populating-next-right-pointers-in-each-node-ii: 二级指针代码虽然简洁优雅,但是对性能有影响,不如一级指针加if else判断快。
力扣第七题通常是指“重建二叉树”(重建二叉树的英文原题可能是“Populating Next Right Pointers in Each Node”)。这是一道中等难度的题目,主要考察的是树结构和深度优先搜索(DFS)或广度优先搜索(BFS)的...
四平方和定理 leetcode Leetcode practice Table of content Tree 92.reverse-linked-list-ii (反转链表 ...94.binary-tree-inorder-...116.populating-next-right-pointers-in-each-node (填充每个节点的下一个右侧节点