描述
Given a binary tree
struct TreeLinkNode {
int val;
TreeLinkNode *left, *right, *next;
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
};
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:
Given a binary tree
struct TreeLinkNode {
int val;
TreeLinkNode *left, *right, *next;
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
};
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
Aer calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
has two children).
For example, Given the following perfect binary tree,
1
/ \
2 3
/ \ / \
4 5 6 7
Aer calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
#ifndef BINTREE_H_
#define BINTREE_H_
#include<vector>
#include<iostream>
struct BinTree
{
int value;
BinTree *left;
BinTree *right;
BinTree *sbling;
};
BinTree *CreateNode(int data);
void Connect(BinTree *pRoot, BinTree *pLeft, BinTree *pRight);
void PrintOneNode(BinTree *pRoot);
void PrintTree(BinTree *pRoot);
void DestroyTree(BinTree *pRoot);
#endif
#include"BinTree.h"
using namespace std;
BinTree *CreateNode(int data)
{
BinTree *pRoot = new BinTree();
pRoot->value = data;
pRoot->left = pRoot->right =pRoot->sbling= NULL;
return pRoot;
}
void Connect(BinTree *pRoot, BinTree *pLeft, BinTree *pRight)
{
pRoot->left = pLeft;
pRoot->right = pRight;
}
void PrintOneNode(BinTree *pRoot)
{
if (pRoot)
{
cout << pRoot->value << endl;
if (pRoot->left)
cout << pRoot->value << " :left node is:" << pRoot->left->value << endl;
else
cout << pRoot->value << " :left node is null" << endl;
if (pRoot->right)
cout << pRoot->value << " :right node is:" << pRoot->right->value << endl;
else
cout << pRoot->value << " :right node is null" << endl;
if (pRoot->sbling)
cout << pRoot->value << ":sbling node is:" << pRoot->sbling->value << endl;
else
cout << pRoot->value << " :sbling node is null" << endl;
}
else
{
cout << "the node is NULL!" << endl;
}
}
void PrintTree(BinTree *pRoot)
{
PrintOneNode(pRoot);
if (pRoot->left)
PrintTree(pRoot->left);
if (pRoot->right)
PrintTree(pRoot->right);
}
void DestroyTree(BinTree *pRoot)
{
if (!pRoot)
return;
else
{
BinTree *pLeft = pRoot->left;
BinTree *pRight = pRoot->right;
delete pRoot;
DestroyTree(pLeft);
DestroyTree(pRight);
}
}
#include"BinTree.h"
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
//====递归
void Populate1(BinTree *pRoot)
{
if (!pRoot||pRoot->left==NULL)
return;
BinTree *p = pRoot;
while (p)
{
p->left->sbling = p->right;
if (p->sbling)
p->right->sbling = p->sbling->left;
p = p->sbling;
}
Populate1(pRoot->left);
}
//===迭代
void Populate2(BinTree *pRoot)
{
if (!pRoot || pRoot->left == NULL)
return;
BinTree *p = NULL;
BinTree *q = pRoot;
while (q->left)
{
p = q;
while (p)
{
p->left->sbling = p->right;
if (p->sbling)
p->right->sbling = p->sbling->left;
p = p->sbling;
}
q = q->left;
}
}
int main()
{
BinTree *p6 = CreateNode(6);
BinTree *p4 = CreateNode(4);
BinTree *p1 = CreateNode(1);
BinTree *p7 = CreateNode(7);
BinTree *p8 = CreateNode(8);
BinTree *p5 = CreateNode(5);
BinTree *p10 = CreateNode(10);
Connect(p6, p4, p8);
Connect(p4, p1, p7);
Connect(p8, p5, p10);
//Populate1(p6);
Populate2(p6);
PrintTree(p6);
DestroyTree(p6);
}