LeetCode 226. Invert Binary Tree

来源:互联网 发布:mac传照片到iphone 编辑:程序博客网 时间:2024/06/03 20:56

226. Invert Binary Tree

Description

Invert a binary tree.     4   /   \  2     7 / \   / \1   3 6   9to     4   /   \  7     2 / \   / \9   6 3   1

Solution

  • 题目意思就是将一棵二叉树每个节点的儿女倒换
  • 思路很简单,先把当前结点的儿女倒换,然后递归处理儿女,代码如下
class Solution {public:    TreeNode* invertTree(TreeNode* root) {        if (!root) return NULL;        swap(root->left,root->right);        invertTree(root->left);        invertTree(root->right);        return root;    }};
原创粉丝点击