题目

Given a binary tree

  1. struct TreeLinkNode {
  2. TreeLinkNode *left;
  3. TreeLinkNode *right;
  4. TreeLinkNode *next;
  5. }

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,



After calling your function, the tree should look like:

分析

为满二叉树添加线索;

由题目可知,线索是根据层序链接,每一层终止节点线索为空,其余节点的next为其层序的下一个节点;

利用层序遍历解决该问题;类似于LeetCode(102) Binary Tree Level Order Traversal

AC代码

  1. /**
  2. * Definition for binary tree with next pointer.
  3. * struct TreeLinkNode {
  4. * int val;
  5. * TreeLinkNode *left, *right, *next;
  6. * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. void connect(TreeLinkNode *root) {
  12. if (!root)
  13. return;
  14. //定义两个队列,一个存储父节点
  15. queue<TreeLinkNode *> parent;
  16. parent.push(root);
  17. root->next = NULL;
  18. while (!parent.empty())
  19. {
  20. //定义队列存储当前子层
  21. queue<TreeLinkNode*> curLevel;
  22. while (!parent.empty())
  23. {
  24. TreeLinkNode *tmp = parent.front();
  25. parent.pop();
  26. if (tmp->left)
  27. curLevel.push(tmp->left);
  28. if (tmp->right)
  29. curLevel.push(tmp->right);
  30. }//while
  31. parent = curLevel;
  32. while (!curLevel.empty())
  33. {
  34. TreeLinkNode *p = curLevel.front();
  35. curLevel.pop();
  36. if (!curLevel.empty())
  37. p->next = curLevel.front();
  38. else
  39. p->next = NULL;
  40. }//while
  41. }//while
  42. }
  43. };

GitHub测试程序源码

LeetCode(116) Populating Next Right Pointers in Each Node的更多相关文章

  1. LeetCode(117) Populating Next Right Pointers in Each Node II

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

  2. leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...

  3. [LeetCode] 116&117. Populating Next Right Pointers in Each Node I&II_Medium tag: BFS(Dont know why leetcode tag it as DFS...)

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

  4. LeetCode(116):填充同一层的兄弟节点

    Medium! 题目描述: 给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *n ...

  5. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

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

  7. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  8. 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)

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

  9. LeetCode OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点II)

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

随机推荐

  1. [LOJ#10042] 收集雪花

    题目链接: 点我 题目分析: 双指针扫描可以保证在\(O(n)\)的时间复杂度内处理这道题.另外,虽然这个题标签是\(hash\),但是字符串\(hash\)是可以卡掉的,所以建议直接离散化. 维护两 ...

  2. bzoj 5449 序列

    https://www.lydsy.com/JudgeOnline/problem.php?id=5449 话说很早以前做过..算是IDA*的板子吧,一个简单的估价函数就可以过去了 %:pragma ...

  3. bzoj 4597||洛谷P4340 [Shoi2016]随机序列

    https://www.lydsy.com/JudgeOnline/problem.php?id=4597 https://www.luogu.org/problemnew/show/P4340 妄图 ...

  4. 【C#】.net 导出Excel功能

    将DataSet对象导出成Excel文档 一.不带格式控制 void btnExport_Click(object sender, EventArgs e) { IList<string> ...

  5. laravel 错误总结

    1.ReflectionException (-1) Class PhotosController does not exist 原因: 资源路由的问题 ,命名空间要区分大小写,admin首字母要大写 ...

  6. 根据Content获取到ItemsControl中对应的Item

    /// <summary> /// 根据控件的值获取到对应的Item /// </summary> /// <typeparam name="T"&g ...

  7. office 导出问题

    就用程序池右击项目高级设置 应用程序池的项目中的标识改为 LocalSystem 启用32位应用程序设为true或false

  8. localStorage 和 sessionStorage的区别

    存储对象: 在主流浏览器中,添加了html5  Web Storage API 的接口,storage是一个存储对象,它包括会话存储(session storage)或本地存储(local stora ...

  9. 16年毕业的前端er在杭州求职ing

    来杭州也有一两个星期了,这个周末下雨,是在没地去,还是习惯性的打开电脑逛技术论坛,想想也是好久没有更新博文了... 背景 因为曾经看过一篇文章面试分享:一年经验初探阿里巴巴前端社招所以来杭州也是带有目 ...

  10. 利用基于@AspectJ的AOP实现权限控制

    一. AOP与@AspectJ AOP 是 Aspect Oriented Programming 的缩写,意思是面向方面的编程.我们在系统开发中可以提取出很多共性的东西作为一个 Aspect,可以理 ...