标签:
Invert a binary tree.
4 / 2 7 / \ / 1 3 6 9
to
4 / 7 2 / \ / 9 6 3 1
我觉得返回void更好一些,就地翻转。
还是递归实现方便。
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 TreeNode* invertTree(TreeNode* root) { 13 if(root==NULL) return NULL; 14 TreeNode *temp = root->left; 15 root->left = invertTree(root->right); 16 root->right = invertTree(temp); 17 return root; 18 } 19 };
标签:
原文地址:http://www.cnblogs.com/Sean-le/p/4769316.html