116. Populating Next Right Pointers in Each Node
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
#include <iostream> #include <vector> #include <string> #include <sstream> #include <stack> #include <iostream> #include <algorithm> #include <math.h> #include <limits.h> using namespace std; struct TreeLinkNode { int val; TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; TreeLinkNode(int val){ this->val=val; this->left = this->right=this->next = NULL; } }; void process(TreeLinkNode *cur,TreeLinkNode *nextNode){ if(cur==NULL) { return; } if(nextNode==NULL) { cur->next=NULL; return; } cur->next=nextNode; process(cur->left,cur->right); process(cur->right,cur->next->left); } void preProcess(TreeLinkNode *root){//预处理,先将右边节点 指向自己。 if(root == NULL) return; root->next=root; preProcess(root->right); } void afterProcess(TreeLinkNode *root){//后处理,先将右边节点 指向NULL。 if(root == NULL) return; root->next=NULL; afterProcess(root->right); } void connect(TreeLinkNode *root) { if(root == NULL) return; preProcess(root); process(root->left,root->right); process(root->right,root->next->left); afterProcess(root); } int main(){ TreeLinkNode* root=new TreeLinkNode(0);//test1 root->left=new TreeLinkNode(1); root->right=new TreeLinkNode(2); root->left->left=new TreeLinkNode(3); root->left->right=new TreeLinkNode(4); root->right->left=new TreeLinkNode(5); root->right->right=new TreeLinkNode(6); connect(root); cout<< "maxPathSum(root)="<<endl; system("pause"); }