Invert Binary Tree(easy)

原创 2015年11月20日 22:20:59

1.直接把递归把左右子树翻转即可


AC代码:

/**
 * 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:
    void invert(TreeNode* root)
    {
    	if (root != NULL)
    	{
    		swap(root->left, root->right);
    		invert(root->left);
    		invert(root->right);
    	}
    	
    }
    TreeNode* invertTree(TreeNode* root) {
            invert(root);
            return root;
    }
    
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

226. Invert Binary Tree [easy] (Python)

题目链接https://leetcode.com/problems/invert-binary-tree/题目原文 Invert a binary tree. 4 / \ ...

[LeetCode] Invert Binary Tree - 二叉树翻转系列问题

目录: Binary Tree Level Order Traversal - 二叉树层次遍历 BFS 题目背景是MaxHowell(他是苹果电脑最受欢迎的homebrew程序作者)去Google面试...

leetCode 226. Invert Binary Tree

题目链接:https://leetcode.com/problems/invert-binary-tree/     题目内容: Invert a binary tree. 4...

leetcode-226. Invert Binary Tree c++ java

1、来源:226. Invert Binary Tree 2、题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 ...

1102. Invert a Binary Tree (25)

唯一注意一点,就是根结点这里并未给出,需要自己根据输入判断出来。 #include #include #include #include #include #include usi...

1102. Invert a Binary Tree (25)-PAT甲级真题

1102. Invert a Binary Tree (25) The following is from Max Howell @twitter: Google: 90% of ou...

LeetCode 226 Invert Binary Tree(转换二叉树)

翻译将下图中上面的二叉树转换为下面的形式,具体为每个左孩子节点和右孩子节点互换位置。原文如上图分析每次关于树的题目出错都在于边界条件上……所以这次仔细多想了一遍:void swapNode(TreeN...
  • NoMasp
  • NoMasp
  • 2015-12-27 12:31
  • 2499

A1102. Invert a Binary Tree (25)

A1102. Invert a Binary Tree (25)

LeetCode Invert Binary Tree OJ 刷题 算法

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

PAT (Advanced Level) 1102. Invert a Binary Tree (25) 解题报告

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)