Invert Binary Tree(easy)

来源:互联网 发布:爱因斯坦人工智能名言 编辑:程序博客网 时间:2024/04/19 20:07

1.直接把递归把左右子树翻转即可


AC代码:

/** * 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:    void invert(TreeNode* root)    {    if (root != NULL)    {    swap(root->left, root->right);    invert(root->left);    invert(root->right);    }        }    TreeNode* invertTree(TreeNode* root) {            invert(root);            return root;    }    };


0 0
原创粉丝点击