LeetCode226:Invert Binary Tree 二叉树反转
题目
QuestionEditorial Solution
My Submissions
Total Accepted:109341
Total Submissions:230799
Difficulty:Easy
Invert a binary tree.
4/\27/\/\1369
to
4/\72/\/\9631
Trivia:
This problem was inspired bythis original tweetbyMax Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
/***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*TreeNode*right;*TreeNode(intx):val(x),left(NULL),right(NULL){}*};*/classSolution{public:TreeNode*invertTree(TreeNode*root){TreeNode*p,*q;p=NULL;q=NULL;if(root!=NULL){p=root->left;q=root->right;root->left=q;root->right=p;invertTree(root->left);invertTree(root->right);}returnroot;}};
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。