116. Populating Next Right Pointers in Each Node
2015-08-12 14:17
579 查看
Given a binary tree
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指针连接旁边的节点,我们可以发现一些规律。
如果一个子节点是根节点的左子树,那么它的next就是该根节点的右子树,譬如上面例子中的4,它的next就是2的右子树5。
如果一个子节点是根节点的右子树,那么它的next就是该根节点next节点的左子树。譬如上面的5,它的next就是2的next(也就是3)的左子树。
此题的关键是从顶向下,一层层地连接各层的节点,然后再沿着已经连接好的某层节点,连接下一层的节点。
所以代码如下:
15.9.14更新
------------------
诚如红色字体所说的:此题的关键是从顶向下,在第一层时候连接第二层的节点,在第二层的时候连接第三层的节点。在算法中,要处理好如何进入下一层,以及当前层结束的标志。
有意思的是,此题在思考的时候,需要想清楚最后两层,而不是像dfs那样的想清楚头两层。
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
这题需要在一棵完全二叉树中使用next指针连接旁边的节点,我们可以发现一些规律。
如果一个子节点是根节点的左子树,那么它的next就是该根节点的右子树,譬如上面例子中的4,它的next就是2的右子树5。
如果一个子节点是根节点的右子树,那么它的next就是该根节点next节点的左子树。譬如上面的5,它的next就是2的next(也就是3)的左子树。
此题的关键是从顶向下,一层层地连接各层的节点,然后再沿着已经连接好的某层节点,连接下一层的节点。
所以代码如下:
class Solution { public: void connect(TreeLinkNode *root) { if(!root) { return; } TreeLinkNode* p = root; TreeLinkNode* first = NULL; while(p) { //记录下层第一个左子树 if(!first) { first = p->left; } //如果有左子树,那么next就是父节点的右子树 if(p->left) { p->left->next = p->right; } else { //叶子节点了,遍历结束 break; } //如果有next,那么设置右子树的next if(p->next) { p->right->next = p->next->left; p = p->next; continue; } else { //转到下一层 p = first; first = NULL; } } } }
15.9.14更新
------------------
诚如红色字体所说的:此题的关键是从顶向下,在第一层时候连接第二层的节点,在第二层的时候连接第三层的节点。在算法中,要处理好如何进入下一层,以及当前层结束的标志。
有意思的是,此题在思考的时候,需要想清楚最后两层,而不是像dfs那样的想清楚头两层。
相关文章推荐
- 利用promise写出更加优美的nodejs程序
- 在 Node.js 中用 Q 实现 Promise – Callback 之外的另一种选择
- NodeJs教程
- Node.js async in practice: When to use what?
- 在Node.js中使用promise摆脱回调金字塔
- Node.js 常见面试题
- Leetcode#19||Remove Nth Node From End of List
- node.js--Less
- [转]Nodejs开发框架Express4.x开发手记
- LeetCode题解:Delete Node in a Linked List
- Remove Nth Node From End of List
- HtmlParser初探--使用Nodefilter和Visitor进行网页分析
- [LeetCode] Count Complete Tree Nodes
- [leetcode-116]Populating Next Right Pointers in Each Node(c++)
- cassandra nodejs driver maillist link
- Node.js详细解析
- 【LeetCode-面试算法经典-Java实现】【116-Populating Next Right Pointers in Each Node(二叉树链接右指针)】
- 我用的一些Node.js开发工具、开发包、框架等总结
- HDU 4587 TWO NODES 枚举+割点
- HDFS源码分析(一)-----INode文件节点