1.根据前序遍历和中序遍历构造二叉树
代码:
/*
* =====================================================================================
*
* Filename: main.cpp
*
* Description: Conding Interviews P55 Construct a Tree
*
* Version: 1.0
* Created: 11/16/2013 07:49:58 PM
* Revision: none
* Compiler: gcc
*
* Author: Chen Hao (), chenhaoustc@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <iostream>
#include <exception>
#include <cstdlib>
using namespace std;
struct BinaryTreeNode
{
int value;
BinaryTreeNode * left;
BinaryTreeNode * right;
};
BinaryTreeNode * ConstructBinaryTree(int *, int * ,int * , int *);
/*
* === FUNCTION ======================================================================
* Name: Construct(int *preOrder,int *inOrder,int num)
* Description:
* =====================================================================================
*/
BinaryTreeNode *
Construct(int *preOrder,int *inOrder,int num)
{
if( preOrder == NULL || inOrder == NULL || num == 0 )
return NULL;
else
{
return ConstructBinaryTree ( preOrder, preOrder + num - 1, inOrder, inOrder + num - 1 );
}
} /* ----- end of function Construct(int *preOrder,int *inOrder,int num) ----- */
/*
* === FUNCTION ======================================================================
* Name: ConstructBinaryTree
* Description:
* =====================================================================================
*/
BinaryTreeNode *
ConstructBinaryTree (int * preOrder,int * preOrderEnd, int * inOrder, int *inOrderEnd )
{
int rootVlue = preOrder[0];
BinaryTreeNode *root = new BinaryTreeNode;
root -> value = rootVlue;
root -> left = root -> right = NULL;
if( preOrder == preOrderEnd )
{
if( inOrder == inOrderEnd && *preOrder == *inOrder )
return root;
}
//find the position of inOrder
int *rootPosition = inOrder;
while( rootPosition <= inOrderEnd && *rootPosition != rootVlue )
++rootPosition;
if( rootPosition == inOrderEnd && *rootPosition != rootVlue )
return NULL;
int leftLength = rootPosition - inOrder;
int * leftPreOrderNew = preOrder + leftLength;
if( leftLength > 0 )
{
//Construct the left tree
root -> left = ConstructBinaryTree( preOrder + 1 , leftPreOrderNew ,inOrder ,rootPosition - 1 );
}
if( leftLength < preOrderEnd - preOrder )
{
//Construct the right tree
root -> right = ConstructBinaryTree( leftPreOrderNew + 1,preOrderEnd, rootPosition + 1,inOrderEnd );
}
return root;
} /* ----- end of function ConstructBinaryTree ----- */
/*
* === FUNCTION ======================================================================
* Name: PrePrint
* Description:
* =====================================================================================
*/
void
PrePrint ( BinaryTreeNode * root )
{
if( root != NULL )
{
cout << root -> value << " ";
PrePrint( root -> left );
PrePrint( root -> right );
}
} /* ----- end of function PrePrint ----- */
/*
* === FUNCTION ======================================================================
* Name: InPrint
* Description:
* =====================================================================================
*/
void
InPrint ( BinaryTreeNode * root )
{
if( root != NULL )
{
InPrint( root -> left );
cout << root -> value << " ";
InPrint( root -> right );
}
} /* ----- end of function InPrint ----- */
/*
* === FUNCTION ======================================================================
* Name: Print
* Description:
* =====================================================================================
*/
void
Print( BinaryTreeNode * root )
{
if( root != NULL )
{
Print( root -> left );
Print( root -> right );
cout << root -> value << " ";
}
} /* ----- end of function Print ----- */
/*
* === FUNCTION ======================================================================
* Name: main
* Description:
* =====================================================================================
*/
int main ( )
{
int pre[8] = { 1,2,4,7,3,5,6,8 };
int in[8] = { 4,7,2,1,5,3,8,6 };
BinaryTreeNode * root = Construct( pre, in, 8 );
PrePrint( root );
cout << endl;
InPrint( root );
cout << endl;
Print( root );
return 1;
} /* ---------- end of function main ---------- */
2.打印二进制中1的个数
代码:
/*
* =====================================================================================
*
* Filename: main.cpp
*
* Description:
*
* Version: 1.0
* Created: 11/17/2013 02:58:19 PM
* Revision: none
* Compiler: gcc
*
* Author: Chen Hao (), chenhaoustc@gmail.com
* Organization:
*
* =====================================================================================
*/
#include<iostream>
using namespace std;
/*
* === FUNCTION ======================================================================
* Name: Numberof1
* Description:
* =====================================================================================
*/
int
Numberof1 ( int n )
{
int count = 0;
while( n )
{
if( n & 1 )
count++;
n = n >> 1;// 右移一位,左边补0
}
return count;
} /* ----- end of function Numberof1 ----- */
/*
* === FUNCTION ======================================================================
* Name: NumberOf1_2
* Description:
* =====================================================================================
*/
int
NumberOf1_2 ( int n )
{
int count = 0;
while( n )
{
++count;
n = n & ( n - 1);//和比自己小1的数做与运算就会使得二进制中少一个一
}
return count ;
} /* ----- end of function NumberOf1_2 ----- */
/*
* === FUNCTION ======================================================================
* Name: NumberOf1_3
* Description:
* =====================================================================================
*/
int
NumberOf1_3 ( int n )//第一种方法如果是负数,则会陷入到死循环
{
int count = 0;
unsigned int flag = 1;
while( flag )
{
if( n & flag )
count ++;
flag = flag << 1;
}
return count ;
} /* ----- end of function NumberOf1_3 ----- */
/*
* === FUNCTION ======================================================================
* Name: main
* Description:
* =====================================================================================
*/
int
main ( int argc, char *argv[] )
{
int n;
while( cin >> n )
{
cout << "The number of 1 of " << n << " is " << NumberOf1_3 ( n ) << endl;
}
} /* ---------- end of function main ---------- */
相关题目:
1.用一条语句判断一个整数是不是2的整数次方。
if( n & ( n - 1 ) )
如果为真,则不是,如果为假,则是2的整数次方
2.输入两个整数m和n,计算需要改变m的二进制表示中的多少位才能变成n
解答:
先让m和n 异或,计算出来m和n 不同的位数(不同的位数为1,相同的为0),然后计算异或后结果中1的个数
代码:
/*
* =====================================================================================
*
* Filename: main1.cpp
*
* Description:
*
* Version: 1.0
* Created: 11/17/2013 03:49:45 PM
* Revision: none
* Compiler: gcc
*
* Author: Chen Hao (), chenhaoustc@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <iostream>
using namespace std;
/*
* === FUNCTION ======================================================================
* Name: Calculate1OfNumber
* Description:
* =====================================================================================
*/
int
Calculate1OfNumber ( int n )
{
int count = 0;
while( n )
{
count ++;
n = n & ( n - 1 );
}
return count ;
} /* ----- end of function Calculate1OfNumber ----- */
/*
* === FUNCTION ======================================================================
* Name: main
* Description:
* =====================================================================================
*/
int
main ( int argc, char *argv[] )
{
int m, n;
int num;
while( cin >> m >> n)
{
int result = m ^ n;
num = Calculate1OfNumber( result );
cout << num << endl;
}
return 0;
} /* ---------- end of function main ---------- */
把一个整数减去1之后在和原来的整数做位与运算,得到的结果相当于是把整数的二进制表示中的最右边的一个1变为0,很多二进制中的问题都可以用这个思路解决。