当前位置: 代码迷 >> 综合 >> 226. Invert Binary Tree二叉树镜像
  详细解决方案

226. Invert Binary Tree二叉树镜像

热度:60   发布时间:2024-01-10 02:52:12.0

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;}
};

 

  相关解决方案