Algorithms—226.Summary Ranges

来源:互联网 发布:怀来大数据产业园 编辑:程序博客网 时间:2024/06/11 16:19

思路:左右互换,递归做。

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

耗时:308ms,中下游。leetcode也会卖萌:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.


0 0