本文主要是介绍226. Invert Binary Tree二叉树镜像,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Invert a binary tree.
Example:
Input:
4/ \2 7/ \ / \ 1 3 6 9
Output:
4/ \7 2/ \ / \ 9 6 3 1
题目链接:https://leetcode.com/problems/invert-binary-tree/
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:void Invert(TreeNode *root){if(root){TreeNode *tmp=root->left;root->left=root->right;root->right=tmp;Invert(root->left);Invert(root->right);}}TreeNode* invertTree(TreeNode* root) {if(root){Invert(root);}return root;}
};
这篇关于226. Invert Binary Tree二叉树镜像的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!