leetcode Populating Next Right Pointers in Each Node(*)
来源:互联网 发布:商业拓展 数据分析 编辑:程序博客网 时间:2024/05/19 02:03
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 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 / \ / \考察观察变通能力,举例如下:对于1来说,1->left->next=1->right;此时,我们可以立足于第二层2->next=3来遍历第二层的链表,使得4->5->6->7,然后立足第三层向下遍历;4->5->6->7 -> NULL
/** * 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) { if(root==NULL)return; TreeLinkNode* temp=root; if(temp->left)temp->left->next=temp->right; while(temp->next&&temp->left){ temp->right->next=temp->next->left; temp=temp->next; temp->left->next=temp->right; } connect(root->left); }};
0 0
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- leetcode Populating Next Right Pointers in Each Node &&Populating Next Right Pointers in Each Node||
- LeetCode Populating Next Right Pointers in Each Node & Populating Next Right Pointers in Each Node I
- 【Leetcode】Populating Next Right Pointers in Each Node in JAVA
- LeetCode Populating Next Right Pointers in Each Node
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node
- LeetCode: Populating Next Right Pointers in Each Node
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node
- [LeetCode] Populating Next Right Pointers in Each Node II
- 【leetcode】 Populating Next Right Pointers in Each Node
- leetcode 78: Populating Next Right Pointers in Each Node
- leetcode 79: Populating Next Right Pointers in Each Node II
- Leetcode: Populating Next Right Pointers in Each Node
- [Leetcode] Populating Next Right Pointers in Each Node ii
- leetcode Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node
- OpenCv截取图像
- Python条件语句Python条件语句是通过一条或多条语句的执行结果
- 华为机试整理[2014年8月15日上午机试准备]
- 本方法使用于V9.7.06以上版本
- ao硬件加速的一个接口和使用(IGlobalScreenDisplaySettings)
- leetcode Populating Next Right Pointers in Each Node(*)
- Spring 3之MVC & Security简单整合开发(一)
- poj 3026 Borg Maze BFS加上最短路
- Web安全渗透自学
- Python多线程
- sencha touch权威指南---学习记录3-面板套面板
- iBatis 动态sql
- 对ChiMerge问题的解析与程序实现(matlab)
- 给刚入职开发者的几句心里话