LeetCode 116. Populating Next Right Pointers in Each Node
来源:互联网 发布:实用的app软件 编辑:程序博客网 时间:2024/06/05 12:27
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 / \ / \ 4->5->6->7 -> NULLanswer:
class Solution {public: void connect(TreeLinkNode *root) { TreeLinkNode temp(0); TreeLinkNode * index = &temp; TreeLinkNode * pre = root, * end = root, * end2 = end; index->next = root; if(!root) return; while(index != end){ index = index->next; pre->next = NULL; if(!index) return; end2->next = index->left; end2 = end2->next; if(!end2) return; end2->next = index->right; end2 = end2->next; if(!end2) return; if(index == end){ pre = end; end = end2; } } } };
0 0
- LeetCode 116. Populating Next Right Pointers in Each Node
- [LeetCode]116.Populating Next Right Pointers in Each Node
- [Leetcode] 116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [leetcode] 116.Populating Next Right Pointers in Each Node
- 116. Populating Next Right Pointers in Each Node LeetCode
- leetcode 116. Populating Next Right Pointers in Each Node
- Leetcode 116. Populating Next Right Pointers in Each Node
- LeetCode *** 116. Populating Next Right Pointers in Each Node
- leetcode-116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [LeetCode]problem 116. Populating Next Right Pointers in Each Node
- Leetcode 116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [leetcode] 116. Populating Next Right Pointers in Each Node
- leetcode-java-116. Populating Next Right Pointers in Each Node
- [LeetCode] 116. Populating Next Right Pointers in Each Node
- 【Leetcode】116. Populating Next Right Pointers in Each Node
- Android Things专题3 开发环境搭建
- Vue.js基础指令、基本介绍
- POJ 1113 Wall
- URL&HTTP协议
- 一个程序看懂C++ switch 执行流程
- LeetCode 116. Populating Next Right Pointers in Each Node
- 编译工具之cmake
- Mongodb数据同步到Hive
- 大二FLAG
- Android之解析JSON数据(android原生态,FastJson,Gson)
- android 自定义多功能进度条
- tmpfs文件系统
- 如何启动和停止 SQL Server Browser 服务
- Hello World