LeetCode之226. Invert Binary Tree

Invert a binary tree.

Example:

Input:

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

Output:

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

自己的代码:(递归)

Runtime: 0 ms, faster than 100.00% of Java online submissions for Invert Binary Tree.

Memory Usage: 35.7 MB, less than 34.90% of Java online submissions for Invert Binary Tree.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return null;

		TreeNode temp = root.left;
		root.left = root.right;
		root.right = temp;
		
		root.left = invertTree(root.left);
		root.right = invertTree(root.right);
		
		return root;
    }
}

 

别人的代码:

Java:

方法一:(层序遍历,BFS)

Runtime: 0 ms, faster than 100.00% of Java online submissions for Invert Binary Tree.

Memory Usage: 35.5 MB, less than 88.64% of Java online submissions for Invert Binary Tree.

public class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return root;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode node = queue.poll();
            TreeNode tmp = node.left;
            node.left = node.right;
            node.right = tmp;
            if(node.left != null) queue.offer(node.left);
            if(node.right != null) queue.offer(node.right);
        }
        return root;
    }
}

小结:

a)核心是:利用队列实现二叉树的层序遍历;

 

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:
            root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
            return root

注意:

a)把上面的那一行,写成两行代码:

     root.left = self.invertTree(root.right)
     root.right = self.invertTree(root.left)

     就无法AC了,原因未知;

方法二:BFS,层序遍历

# BFS
def invertTree2(self, root):
    queue = collections.deque([(root)])
    while queue:
        node = queue.popleft()
        if node:
            node.left, node.right = node.right, node.left
            queue.append(node.left)
            queue.append(node.right)
    return root

 

方法三:DFS

# DFS
def invertTree(self, root):
    stack = [root]
    while stack:
        node = stack.pop()
        if node:
            node.left, node.right = node.right, node.left
            stack.extend([node.right, node.left])
    return root

小结:

a)使用Python中的列表[ ]来作为数据结构栈stack

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值