23 二叉树的前序遍历
# include <vector>
class Solution {
public :
vector< int > ans;
void show ( TreeNode* r) {
if ( ! r) return ;
ans. push_back ( r-> val) ;
show ( r-> left) ;
show ( r-> right) ;
}
vector< int > preorderTraversal ( TreeNode* root) {
show ( root) ;
return ans;
}
} ;
24 二叉树的中序遍历
class Solution {
public :
vector< int > ans;
void show ( TreeNode* r) {
if ( ! r) return ;
show ( r-> left) ;
ans. push_back ( r-> val) ;
show ( r-> right) ;
}
vector< int > inorderTraversal ( TreeNode* root) {
show ( root) ;
return ans;
}
} ;
25 二叉树的后序遍历
class Solution {
public :
vector< int > ans;
void show ( TreeNode* r) {
if ( ! r) return ;
show ( r-> left) ;
show ( r-> right) ;
ans. push_back ( r-> val) ;
}
vector< int > postorderTraversal ( TreeNode* root) {
show ( root) ;
return ans;
}
} ;
26 求二叉树的层序遍历
# include <cstddef>
# include <queue>
# include <vector>
class Solution {
public :
vector< vector< int > > levelOrder ( TreeNode* root) {
vector< vector< int > > ans;
vector< int > Temp;
queue< TreeNode* > Q;
if ( ! root) return ans;
Q. push ( root) ;
Temp. push_back ( root-> val) ;
ans. push_back ( Temp) ;
while ( Q. size ( ) ) {
queue< TreeNode* > TQ;
Temp. clear ( ) ;
int m = ans. size ( ) ;
while ( Q. size ( ) ) {
TreeNode* cur = Q. front ( ) ; Q. pop ( ) ;
if ( cur-> left) {
Temp. push_back ( cur-> left-> val) ;
TQ. push ( cur-> left) ;
}
if ( cur-> right) {
Temp. push_back ( cur-> right-> val) ;
TQ. push ( cur-> right) ;
}
}
if ( Temp. size ( ) )
ans. push_back ( Temp) ;
Q = TQ;
}
return ans;
}
} ;
27 按之字形顺序打印二叉树
class Solution {
public :
vector< vector< int > > Print ( TreeNode* pRoot) {
vector< vector< int > > ans;
vector< int > Temp;
queue< TreeNode* > Q;
if ( ! pRoot) return ans;
Q. push ( pRoot) ;
Temp. push_back ( pRoot-> val) ;
ans. push_back ( Temp) ;
while ( Q. size ( ) ) {
queue< TreeNode* > TQ;
Temp. clear ( ) ;
int m = ans. size ( ) ;
while ( Q. size ( ) ) {
TreeNode* cur = Q. front ( ) ; Q. pop ( ) ;
if ( cur-> left) {
Temp. push_back ( cur-> left-> val) ;
TQ. push ( cur-> left) ;
}
if ( cur-> right) {
Temp. push_back ( cur-> right-> val) ;
TQ. push ( cur-> right) ;
}
}
if ( Temp. size ( ) ) {
if ( ans. size ( ) % 2 == 1 ) {
vector< int > e;
for ( int i= Temp. size ( ) - 1 ; i>= 0 ; i-- )
e. push_back ( Temp[ i] ) ;
ans. push_back ( e) ;
} else {
ans. push_back ( Temp) ;
}
}
Q = TQ;
}
return ans;
}
} ;
28 二叉树的最大深度
class Solution {
public :
int awm ( TreeNode* T) {
if ( ! T) return 0 ;
return max ( awm ( T-> left) , awm ( T-> right) ) + 1 ;
}
int maxDepth ( TreeNode* root) {
return awm ( root) ;
}
} ;
29 二叉树中和为某一值的路径(一)
class Solution {
public :
bool awm ( TreeNode* T, int sum) {
if ( ! T)
return false ;
sum -= T-> val;
if ( ! T-> left && ! T-> right && sum == 0 )
return true ;
return awm ( T-> left, sum) || awm ( T-> right, sum) ;
}
bool hasPathSum ( TreeNode* root, int sum) {
return awm ( root, sum) ;
}
} ;
30 二叉搜索树与双向链表
class Solution {
public :
TreeNode* head;
TreeNode* pre;
TreeNode* Convert ( TreeNode* pRootOfTree) {
if ( ! pRootOfTree)
return NULL ;
Convert ( pRootOfTree-> left) ;
if ( head == NULL ) {
head = pRootOfTree;
pre = pRootOfTree;
} else {
pre-> right = pRootOfTree;
pRootOfTree-> left = pre;
pre = pRootOfTree;
}
Convert ( pRootOfTree-> right) ;
return head;
}
} ;
31 对称的二叉树
class Solution {
public :
bool awm ( TreeNode* T1, TreeNode* T2) {
if ( ! T1 && ! T2)
return true ;
if ( ! T1 || ! T2 || T1-> val!= T2-> val)
return false ;
return awm ( T1-> left, T2-> right) && awm ( T1-> right, T2-> left) ;
}
bool isSymmetrical ( TreeNode* pRoot) {
if ( ! pRoot) return true ;
return awm ( pRoot-> left, pRoot-> right) ;
}
} ;
32 合并二叉树
class Solution {
public :
TreeNode* mergeTrees ( TreeNode* t1, TreeNode* t2) {
if ( ! t1)
return t2;
if ( ! t2)
return t1;
TreeNode* node = new TreeNode ( t1-> val+ t2-> val) ;
node-> left = mergeTrees ( t1-> left, t2-> left) ;
node-> right = mergeTrees ( t1-> right, t2-> right) ;
return node;
}
} ;
33 二叉树的镜像
class Solution {
public :
TreeNode* Mirror ( TreeNode* pRoot) {
if ( ! pRoot)
return NULL ;
TreeNode* temp = pRoot-> right;
pRoot-> right = pRoot-> left;
pRoot-> left = temp;
if ( pRoot-> left)
Mirror ( pRoot-> left) ;
if ( pRoot-> right)
Mirror ( pRoot-> right) ;
return pRoot;
}
} ;
34 判断是不是二叉搜索树
# include <climits>
class Solution {
public :
long pre = INT_MIN;
bool isValidBST ( TreeNode* root) {
if ( ! root) return true ;
if ( ! isValidBST ( root-> left) )
return false ;
if ( root-> val <= pre)
return false ;
pre = root-> val;
if ( ! isValidBST ( root-> right) )
return false ;
return true ;
}
} ;
35 判断是不是完全二叉树
# include <queue>
class Solution {
public :
bool isCompleteTree ( TreeNode* root) {
if ( ! root) return true ;
int p = 0 ;
queue< TreeNode* > Q;
Q. push ( root) ;
while ( Q. size ( ) ) {
TreeNode* node = Q. front ( ) ;
Q. pop ( ) ;
if ( p == 1 && node)
return false ;
if ( ! node) {
p = 1 ;
} else {
Q. push ( node-> left) ;
Q. push ( node-> right) ;
}
}
return true ;
}
} ;
36 判断是不是平衡二叉树
# include <algorithm>
class Solution {
public :
int awm ( TreeNode* t) {
if ( ! t) return 0 ;
int a = awm ( t-> left) ;
int b = awm ( t-> right) ;
if ( a== - 1 || b== - 1 )
return - 1 ;
int c = abs ( a- b) ;
if ( c == 0 || c == 1 )
return max ( a, b) + 1 ;
else
return - 1 ;
}
bool IsBalanced_Solution ( TreeNode* pRoot) {
int ans = awm ( pRoot) ;
return ans!= - 1 ;
}
} ;
37 二叉搜索树的最近公共祖先
# include <algorithm>
# include <stack>
# include <vector>
class Solution {
public :
TreeNode* awm ( TreeNode* T, int p, int q) {
if ( ! T || T-> val == p || T-> val == q)
return T;
TreeNode* left = NULL ;
TreeNode* right = NULL ;
if ( T-> val < min ( p, q) ) {
left = NULL ;
} else {
left = awm ( T-> left, p, q) ;
}
if ( T-> val > max ( p, q) ) {
right = NULL ;
} else {
right = awm ( T-> right, p, q) ;
}
if ( ! left)
return right;
if ( ! right)
return left;
return T;
}
int lowestCommonAncestor ( TreeNode* root, int p, int q) {
return awm ( root, p, q) -> val;
}
} ;
38 在二叉树中找到两个节点的最近公共祖先
class Solution {
public :
TreeNode* awm ( TreeNode* T, int o1, int o2) {
if ( ! T || T-> val == o1 || T-> val == o2)
return T;
TreeNode* left = awm ( T-> left, o1, o2) ;
TreeNode* right = awm ( T-> right, o1, o2) ;
if ( ! left)
return right;
if ( ! right)
return left;
return T;
}
int lowestCommonAncestor ( TreeNode* root, int o1, int o2) {
return awm ( root, o1, o2) -> val;
}
} ;
39 序列化二叉树
# include <cstddef>
# include <string>
# include <vector>
class Solution {
public :
char * Serialize ( TreeNode* root) {
if ( ! root) return "$" ;
string ans = "" ;
queue< TreeNode* > Q;
Q. push ( root) ;
while ( Q. size ( ) ) {
TreeNode* node = Q. front ( ) ;
Q. pop ( ) ;
if ( node) {
ans += to_string ( node-> val) + "|" ;
Q. push ( node-> left) ;
Q. push ( node-> right) ;
} else {
ans += "#|" ;
}
}
ans += "$" ;
cout << ans << endl;
char * C = new char [ ans. length ( ) + 1 ] ;
strcpy ( C, ans. c_str ( ) ) ;
C[ ans. length ( ) ] = '\0' ;
return C;
}
TreeNode* awm ( int p, vector< int > V) {
cout << V[ p] << endl;
if ( V[ p] == - 1 || p >= V. size ( ) ) return NULL ;
TreeNode* node = new TreeNode ( V[ p] ) ;
node-> left = awm ( p * 2 , V) ;
node-> right = awm ( p * 2 + 1 , V) ;
return node;
}
TreeNode* Deserialize ( char * str) {
if ( str[ 0 ] == '$' ) return NULL ;
vector< int > V;
V. push_back ( - 1 ) ;
int p = 0 ;
while ( str[ p] != '$' ) {
if ( str[ p] == '#' ) {
V. push_back ( - 1 ) ;
p++ ;
} else {
int num = 0 ;
while ( str[ p] != '$' && str[ p] != '|' ) {
num *= 10 ;
num += str[ p] - '0' ;
p++ ;
}
V. push_back ( num) ;
}
p++ ;
}
for ( int i = 1 ; i < V. size ( ) ; i++ ) {
if ( V[ i] == - 1 ) {
if ( i * 2 < V. size ( ) ) {
V. insert ( V. begin ( ) + i * 2 , - 1 ) ;
V. insert ( V. begin ( ) + i * 2 + 1 , - 1 ) ;
}
}
}
for ( int i = 0 ; i < V. size ( ) ; i++ )
cout << V[ i] << " " ;
cout << endl;
return awm ( 1 , V) ;
}
} ;
40 重建二叉树
# include <vector>
class Solution {
public :
TreeNode* reConstructBinaryTree ( vector< int > pre, vector< int > vin) {
int len1 = pre. size ( ) ;
int len2 = vin. size ( ) ;
if ( ! len1 || ! len2)
return NULL ;
TreeNode* node = new TreeNode ( pre[ 0 ] ) ;
for ( int i = 0 ; i < len2; i++ ) {
if ( pre[ 0 ] == vin[ i] ) {
vector< int > lPre ( pre. begin ( ) + 1 , pre. begin ( ) + i + 1 ) ;
vector< int > lVin ( vin. begin ( ) , vin. begin ( ) + i) ;
node-> left = reConstructBinaryTree ( lPre, lVin) ;
vector< int > rPre ( pre. begin ( ) + 1 + i, pre. end ( ) ) ;
vector< int > rVin ( vin. begin ( ) + i + 1 , vin. end ( ) ) ;
node-> right = reConstructBinaryTree ( rPre, rVin) ;
break ;
}
}
return node;
}
} ;
41 输出二叉树的右视图
# include <queue>
# include <vector>
class Solution {
public :
TreeNode* reConstructBinaryTree ( vector< int > pre, vector< int > vin) {
int len1 = pre. size ( ) ;
int len2 = vin. size ( ) ;
if ( ! len1 || ! len2)
return NULL ;
TreeNode* node = new TreeNode ( pre[ 0 ] ) ;
for ( int i = 0 ; i < len2; i++ ) {
if ( pre[ 0 ] == vin[ i] ) {
vector< int > lPre ( pre. begin ( ) + 1 , pre. begin ( ) + i + 1 ) ;
vector< int > lVin ( vin. begin ( ) , vin. begin ( ) + i) ;
node-> left = reConstructBinaryTree ( lPre, lVin) ;
vector< int > rPre ( pre. begin ( ) + 1 + i, pre. end ( ) ) ;
vector< int > rVin ( vin. begin ( ) + i + 1 , vin. end ( ) ) ;
node-> right = reConstructBinaryTree ( rPre, rVin) ;
break ;
}
}
return node;
}
vector< int > solve ( vector< int > & xianxu, vector< int > & zhongxu) {
TreeNode* root = reConstructBinaryTree ( xianxu, zhongxu) ;
vector< int > ans;
if ( ! root) return ans;
queue< TreeNode* > Q;
Q. push ( root) ;
while ( Q. size ( ) ) {
queue< TreeNode* > temp;
while ( Q. size ( ) ) {
TreeNode* node = Q. front ( ) ; Q. pop ( ) ;
if ( node-> left) temp. push ( node-> left) ;
if ( node-> right) temp. push ( node-> right) ;
if ( ! Q. size ( ) ) {
ans. push_back ( node-> val) ;
Q = temp;
break ;
}
}
}
return ans;
}
} ;