递归交换二叉树左右子树:BinaryTree:Exchange the left child and right child of a binary tree
2009-12-05 11:46
633 查看
// BTree.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
using namespace std;
typedef struct _node
{
int data;
struct _node* rch;
struct _node* lch;
}Node;
Node* InsertNode(Node* n,int d)
{
if(n)
{
if(d>n->data)
n->rch=InsertNode(n->rch,d);
else n->lch=InsertNode(n->lch,d);
}
else
{
n=(Node*)malloc(sizeof(Node));
n->data=d;
n->lch=NULL;
n->rch=NULL;
}
return n;
}
void CreateTree(Node* root,int arr[],int len)
{
for(int i=0;i<len;i++)
{
InsertNode(root,arr[i]);
}
}
void SwapChild(Node* root)
{
if(root)
{
if(root->lch!=NULL||root->rch!=NULL)
{
Node* temp;
temp=root->lch;
root->lch=root->rch;
root->rch=temp;
SwapChild(root->lch);
SwapChild(root->rch);
}
}
}
void InorderTree(Node* root)
{
if(root)
{
InorderTree(root->lch);
cout<<root->data<<" ";
InorderTree(root->rch);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[]={12,34,1,78,54,89};
Node* head=(Node*)malloc(sizeof(Node));
head->data=0;
head->lch=NULL;
head->rch=NULL;
CreateTree(head,a,6);
InorderTree(head);
cin.get();
return 0;
}
//
#include "stdafx.h"
#include "iostream"
using namespace std;
typedef struct _node
{
int data;
struct _node* rch;
struct _node* lch;
}Node;
Node* InsertNode(Node* n,int d)
{
if(n)
{
if(d>n->data)
n->rch=InsertNode(n->rch,d);
else n->lch=InsertNode(n->lch,d);
}
else
{
n=(Node*)malloc(sizeof(Node));
n->data=d;
n->lch=NULL;
n->rch=NULL;
}
return n;
}
void CreateTree(Node* root,int arr[],int len)
{
for(int i=0;i<len;i++)
{
InsertNode(root,arr[i]);
}
}
void SwapChild(Node* root)
{
if(root)
{
if(root->lch!=NULL||root->rch!=NULL)
{
Node* temp;
temp=root->lch;
root->lch=root->rch;
root->rch=temp;
SwapChild(root->lch);
SwapChild(root->rch);
}
}
}
void InorderTree(Node* root)
{
if(root)
{
InorderTree(root->lch);
cout<<root->data<<" ";
InorderTree(root->rch);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[]={12,34,1,78,54,89};
Node* head=(Node*)malloc(sizeof(Node));
head->data=0;
head->lch=NULL;
head->rch=NULL;
CreateTree(head,a,6);
InorderTree(head);
cin.get();
return 0;
}
相关文章推荐
- 非递归交换二叉树左右子树:BinaryTree:Exchange the left child and the right child without recursive method
- travel the binary tree by level 4 ( from down to top and from left to right every level )
- travel the binary tree by level 3 ( from down to top and from right to left every level )
- travel the binary tree by level 5 ( from down to top and from left to right every level )
- Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 [java]
- [Leetcode] The minimum depth of binary tree二叉树的最小深度
- 反转二叉树,即交换所有结点的左右子树,但不能使用递归方法。
- leetcode_104题——Maximum Depth of Binary Tree (二叉树,递归,队列,还有递归没想出来)
- the Differences of Fold-left and Fold-right
- 【LeetCode笔记】Maximum Depth of Binary Tree 二叉树最大深度 递归&非递归
- leetcode-226. Invert Binary Tree 二叉树、递归,交换两个变量的值
- CodeForces 438 E.The Child and Binary Tree(生成函数+FFT)
- 交换二叉树的左右子树——非递归方式
- leetcode 236. Lowest Common Ancestor of a Binary Tree-二叉树共同祖先|深度遍历|递归|非递归
- 6_43_递归交换二叉树中所有节点的左右子树
- Codeforces 250 E. The Child and Binary Tree [多项式开根 生成函数]
- 226.Invert Binary Tree(交换二叉树左右结点)
- 【二叉树的递归】01二叉树的最小深度【Minimum Depth of Binary Tree】
- 5.4.1—二叉树的递归—Minimum Depth of Binary Tree
- 二叉树中所有节点的左右子树相互交换 递归与非递归程序