leetcode116 Populating Next Right Pointers in Each Node
题意:给一个完全二叉树:
1
/ \
2 3
/ \ / \
4 5 6 7
让左子树的next指针指向右子树,右子树的next继续指向右边,变成了这样:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
思路:唉,英语不行真是醉了,半天没弄明白题意,最后没办法了去找了别人的博客看才明白要干啥。这题的特殊点在于树是完全二叉树,对于其中的一个节点p来说,p->left->next = p->right;p->right->next = p->next->left;明白了这两点这题就解决了,可以用递归,即对root进行这样的操作再对root->left和root->right再做一遍;也可以迭代,附上两种方法的代码。
此题不错,应该多注意一下。
代码1 递归:
void connect(TreeLinkNode *root)
{
if(root == NULL)
return;
if(root->next && root->right)
root->right->next = root->next->left;
if(root->left)
root->left->next = root->right;
connect(root->left);
connect(root->right);
}
代码2 迭代:
void connect(TreeLinkNode *root)
{
TreeLinkNode *pre = root;
TreeLinkNode *cur = NULL;
while(pre)
{
cur = pre;
while(cur && cur->left)
{
cur->left->next = cur->right;
if(cur->next)
cur->right->next = cur->next->left;
cur = cur->next;
}
pre = pre->left;
}
}
leetcode116 Populating Next Right Pointers in Each Node的更多相关文章
- Leetcode116. Populating Next Right Pointers in Each Node填充同一层的兄弟节点
给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } 填充它的每个 ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- [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
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 II
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 【leetcode】Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
随机推荐
- nowcoder 提高组模拟赛 最长路 解题报告
最长路 链接: https://www.nowcoder.com/acm/contest/178/A 来源:牛客网 题目描述 有一张 \(n\) 个点 \(m\) 条边的有向图,每条边上都带有一个字符 ...
- 理解[].forEach.call()
例子: let cols = document.querySelectorAll('ul li') [].forEach.call(cols, function (col, index) { // T ...
- Small things are better
Yesterday I had fun time repairing 1.5Tb ext3 partition, containing many millions of files. Of cours ...
- HDU 2844 二进制优化的多重背包
Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- [hdu 3949]线性基+高斯消元
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 一开始给做出来的线性基wa了很久,最后加了一步高斯消元就过了. 之所以可以这样做,证明如下. 首 ...
- fresco的使用教程
1.加载依赖 api 'org.xutils:xutils:3.5.0' 2.创建一个myapplication public class MyApplication extends Applicat ...
- i=i+1与i+=1的区别及效率(Java)
原博客地址 在做个java优化的PPT时,看到了i=i+1与i+=1的区别,在这之前还真没想到那么细. 1.x=x+1,x+=1及x++的效率哪个最高?为什么? x=x+1最低,因为它的执行如下. ( ...
- P3076 [USACO13FEB]出租车Taxi
题目描述 Bessie is running a taxi service for the other cows on the farm. The cows have been gathering a ...
- centos7装机时更改网卡名为eth0操作
- cube中的判断类型
import { createAddAPI } from '../util' const DATE_RE = /^(1|2)\d{3}[.\-/]\d{1,2}[.\-/]\d{1,2}$/ cons ...