C++[Leetcode 题解 / 226] Invert Binary Tree

Homebrew是OS X平台上之保险管理工具,在用Mac的程序员基本都清楚之家伙。
HomeBrew的开发者是Max Howell。然而面试谷歌时可蛋疼了。Max
Howell在Twitter发帖:

twitter

足见,会手写反转二交叉树多重要。正好Leetcode上生其一题材,下面进入正题。

二叉树凡是数据结构里一个重大之概念。
假设反转二叉树的为主意思就是是下图这样。
Invert a binary tree.

     4
   /   \
  2     7
 / \   / \
1   3 6   9

to:

     4
   /   \
  7     2
 / \   / \
9   6 3   1

各个一个节点的左右子树对换,左右子树的横节点吧用交换,这种时刻怪易想到的即使是递归的法子。
脚是在Leetcode 通过之C++代码:

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

至于非递归的做法也大简单,借助一个班就得兑现,在C++里,直接动用标准库的queue就可。
率先得到根节点入队,再出队,交换其的左右节点,再将左右节点入队,这样就足以以层次遍历的办法,处理每一样重合的节点。

/**
 * 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:
    TreeNode* invertTree(TreeNode* root) {
        queue<TreeNode *> node_queue;

        if(root == NULL)
            return root;
        node_queue.push(root);
        while(node_queue.size()>0) 
        {
            TreeNode* pFrontNode = node_queue.front();
            node_queue.pop();
            TreeNode *tmp = pFrontNode->left;
            pFrontNode->left = pFrontNode->right;
            pFrontNode->right = tmp;
            if(pFrontNode->left)
                node_queue.push(pFrontNode->left);
            if(pFrontNode->right)
                node_queue.push(pFrontNode->right);
        }
        return root;

    }
};

近日于看Python,用Python实现啊一律,递归解法:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def invertTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        if root is None:
            return None
        root.left,root.right = self.invertTree(root.right),self.invertTree(root.left)
        return root

非递归

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def invertTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        queue = collections.deque()
        if root:
            queue.append(root)
        while queue:
            node = queue.popleft()
            if node.left:
                queue.append(node.left)
            if node.right:
                queue.append(node.right)
            node.left,node.right = node.right,node.left
        return root