Populating Next Right Pointers in Each Node--LeetCode
来源:互联网 发布:nba2k17季后赛数据 编辑:程序博客网 时间:2024/04/30 15:07
题目:
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
void Connect(BinTree* root){ if(root == NULL) return ; deque<BinTree*> de; de.push_back(root); int i,level=1; BinTree* cur,*pre; while(!de.empty()) { cur = NULL; pre = NULL; for(i=0;i<level&&!de.empty();i++) { if(pre == NULL) { pre = de.front(); if(pre->left != NULL) de.push_back(pre->left); if(pre->right !=NULL) de.push_back(pre->right); de.pop_front(); cout<<pre->value<<endl; continue; } if(cur == NULL) { cur = de.front(); if(cur->left != NULL) de.push_back(cur->left); if(cur->right !=NULL) de.push_back(cur->right); de.pop_front(); cout<<cur->value<<endl; continue; } pre->next =cur; pre = cur; cur = de.front(); de.pop_front(); if(cur->left != NULL) de.push_back(cur->left); if(cur->right != NULL) de.push_back(cur->right); cout<<cur->value<<endl; } if(cur == NULL) pre->next == NULL; else { pre->next = cur; cur->next == NULL; } level *=2; }}
题目的要求是不用额外的空间,加入有一层已经弄好了,那么根据这一层是否可以初始化下一层?可以,从上一层头节点的左边开始即可。最后是改造后的层次遍历
void helper(BinTree* pre_head){ if(pre_head->left == NULL )//|| pre_head->left== NULL) return ; BinTree* cur_first = pre_head->left; BinTree* pre = pre_head; BinTree* cur_second = pre_head->right; while(pre != NULL&&pre->left != NULL) { cur_first->next = cur_second; cur_first = cur_second; pre = pre->next; if(pre != NULL && pre->left != NULL) { cur_second = pre->left; cur_first->next = cur_second; cur_first = cur_second; cur_second = pre->right; } } if(cur_first != NULL) cur_first->next =NULL; helper(pre_head->left);}void Connect_second(BinTree* root){ if(root ==NULL) return ; root->next = NULL; helper(root);}void order(BinTree* root){ BinTree* temp = root; if(temp == NULL) return; while(temp != NULL) { cout<<temp->value<<endl; temp = temp->next; } order(root->left);}
ps:这是一个满二叉树,所以对于每一层节点的个数都是一定的,假如题目中没有要求使用常量的空间,那么仍然可以使用层次遍历的方法来处理这个问题,对于每一层的头节点和每一层的尾节点都是知道的,既然要求使用常量的空间复杂度,那么就需要使用递归了!
对于一个函数,如果传递了当前层次的第一个节点pre,那么就可以处理下面那个层次,因为是一个满二叉树,pre节点横向是一个单链表,由这个单链表可以遍历下面层次的各个节点
1 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
- web学习笔记(三)配置sae服务器
- [准备篇3]VMWare搭建Openstack——规划与预前知识
- R语言 | 多元回归分析中的对照编码(contrast coding) | 第二节 deviation coding(偏差编码)
- (数据结构第二章)静态链表的实现
- Android Hello World以及目录结构说明
- Populating Next Right Pointers in Each Node--LeetCode
- ANDROID基础学习笔记_3.1_文件权限
- 回溯算法--Fire Net
- iOS 笔记《About the iOS Technologies:Cocoa Touch Layer》
- 算法练习-NOJ-1008-素数环问题
- IOS 高级开发 runtime(二)
- Java集合类
- 【One Day菜鸟到大鸟】MyBatis搭建环境
- QWidget与HWND的互相转换