Siukwan Program

http://siukwan.sinaapp.com/

Invert Binary Tree(easy)

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


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jmlikun/article/details/49952719
个人分类: leetcode
上一篇1102. Invert a Binary Tree (25)
下一篇1103. Integer Factorization (30)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭