Invert Binary Tree

Posted summerkiki

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Invert Binary Tree相关的知识,希望对你有一定的参考价值。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(!root)
            return root;
        if(root->left||root->right)
        {
            TreeNode* temp=root->left;
            root->left=root->right;
            root->right=temp;
            if(root->left)
                invertTree(root->left);
            if(root->right)
                invertTree(root->right);
        }
        
        return root;
        
    }
};

 

以上是关于Invert Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode]226.Invert Binary Tree

PAT1102: Invert a Binary Tree

letecode [226] - Invert Binary Tree

226. Invert Binary Tree

Invert Binary Tree

226. Invert Binary Tree