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 / \ ...

PAT-PAT (Advanced Level) Practise 1102 Invert a Binary Tree (25)【三星级】

题目链接:http://www.patest.cn/contests/pat-a-practise/1102 题面: 1102. Invert a Binary Tree (25) ...

[leetcode] 226. Invert Binary Tree 解题报告

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

【PAT】1102. Invert a Binary Tree (25)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ...

LeetCode笔记:226. Invert Binary Tree

反转二叉树每个节点的左右子节点

1102.Invert a Binary Tree (25)[树的遍历]

1. 原题: https://www.patest.cn/contests/pat-a-practise/1102 2. 思路: 题意:树的遍历题。 给出一颗二叉树,输出反转后的层序和中序。 思路: ...

1102. Invert a Binary Tree 解析

按表建树, 然后层序按 先右子树再左子树的顺序进队列, 中序按 右中左的顺序遍历就是翻转后的。 #include #include #include #include #include ...

pat 1102. Invert a Binary Tree (25)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ...

leetcode 226 :Invert Binary Tree

题目: Invert a binary tree.4 / \ 2 7 / \ / \ 1 3 6 9to4 / \ 7 2 ...

LeetCode题解-226-Invert Binary Tree

尽管写LeetCode解答主要是为了自己整理思路,但是如果对您也正好有帮助,请点个赞给菜鸟一点鼓励吧 :-) 原题 解法分析 解法1使用了递归; 解法2使用了迭代法,为广度优先...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Invert Binary Tree(easy)
举报原因:
原因补充:

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