LeetCode:Populating Next Right Pointers in Each Node
来源:互联网 发布:linux查看普通用户 编辑:程序博客网 时间:2024/05/29 03:37
Total Accepted: 71347 Total Submissions: 196539 Difficulty: Medium
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 -> NULL
code:
/** * 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) { TreeLinkNode *cur; while(root && root->left){ cur=root; while(cur){ cur->left->next=cur->right; if(cur->next) cur->right->next=cur->next->left; cur=cur->next; } root=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
- CodeIgniter框架入门教程-HelloWorld
- php+apc 安装与优化
- Java JDBC封装模式
- 《PHP+MySQL八大动态Web应用实践》源码修正及分享
- CSDN博客第一篇开启
- LeetCode:Populating Next Right Pointers in Each Node
- 端口
- Spring.NET学习笔记1——控制反转(基础篇) Level 200
- Rest API规范
- 软件开发概述
- 有向图(3)--寻找有向环
- Java Swing中图标,背景等最好用的类和方法
- cakephp 框架
- High Performance Mysql 读书笔记——创建高性能索引