美文网首页
leetcode 226. 翻转二叉树

leetcode 226. 翻转二叉树

作者: Source_Chang | 来源:发表于2020-11-01 15:40 被阅读0次

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

    相关文章

      网友评论

          本文标题:leetcode 226. 翻转二叉树

          本文链接:https://www.haomeiwen.com/subject/afxcvktx.html