LCR 144.翻转二叉树

​​题目来源:

        leetcode题目,网址:LCR 144. 翻转二叉树 - 力扣(LeetCode)

解题思路:

       交换所有节点得左右子树即可。

解题代码:

/**
 * 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* mirrorTree(TreeNode* root) {
        if(root==NULL){
            return root;
        }
        mirrorTree(root->left);
        mirrorTree(root->right);
        TreeNode* temp=root->left;
        root->left=root->right;
        root->right=temp;
        return root;
    }

};
 
  

总结:

        无官方题解。

        应该用 nullptr,NULL 可能存在二义性问题。


你可能感兴趣的:(#,C++,LeetCode,C++)