Invert Binary Tree

Description:

Invert a binary tree.

     4
   /   \
  2     7
 / \   / \
1   3 6   9

to

     4
   /   \
  7     2
 / \   / \
9   6 3   1

Code:

 1  TreeNode* invertTree(TreeNode* root) {
 2         if (root==NULL || ( root->left == 0 && root->right == 0))
 3             return root;
 4             
 5         if ( root->left && root->right )
 6         {
 7             TreeNode* temp = root->left;
 8             root->left = root->right;
 9             root->right = temp;
10         }
11         else if ( root->left == NULL)
12         {
13             root->left = root->right;
14             root->right = NULL;
15         }
16         else
17         {
18             root->right = root->left;
19             root->left = NULL;
20         }
21         root->left = invertTree(root->left);
22         root->right = invertTree(root->right);
23         return root;
24     }

总结:对题目没有理解清楚,所以不能独立完成

posted @ 2015-06-19 11:09  Rosanne  阅读(109)  评论(0编辑  收藏  举报