TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Invert a binary tree on LeetCode

34 点作者 ripitrust将近 10 年前
Inspired by Max Howell's tweet

14 条评论

de_Selby将近 10 年前
The problem wasn&#x27;t to just reverse the nodes, it was to re-sort it:<p>&gt; @rogerdai16 to min-max the tree, ascending to descending.<p><a href="https:&#x2F;&#x2F;twitter.com&#x2F;mxcl&#x2F;status&#x2F;608891015945170944" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;mxcl&#x2F;status&#x2F;608891015945170944</a>
评论 #9710605 未加载
评论 #9710861 未加载
评论 #9710954 未加载
评论 #9710728 未加载
mgraczyk将近 10 年前
In fairness to Google, the question is pretty darn simple...<p><pre><code> def invertTree(self, root): if root is not None: tmp = root.left root.left = self.invertTree(root.right) root.right = self.invertTree(tmp) return root</code></pre>
评论 #9710563 未加载
评论 #9710659 未加载
评论 #9710594 未加载
评论 #9710656 未加载
评论 #9710570 未加载
评论 #9710573 未加载
meggar将近 10 年前
This looks like it works<p><pre><code> &lt;html&gt; &lt;style&gt; pre { -webkit-transform:scaleX(-1); -moz-transform:scaleX(-1); -ms-transform:scaleX(-1); -o-transform:scaleX(-1); transform:scaleX(-1); } &lt;&#x2F;style&gt; &lt;pre&gt; 4 &#x2F; \ 2 7 &#x2F; \ &#x2F; \ 1 3 6 9 &lt;&#x2F;pre&gt; &lt;&#x2F;html&gt;</code></pre>
diminish将近 10 年前
What&#x27;s hard is to have necessary CS background to understand what &quot;inverting a tree&quot; as a term means. As a non-CS major, I don&#x27;t know what they use it for as a &quot;term&quot; and it evokes several things I can do with a tree as an outsider. So interviewers can quickly eliminate me as a non-CS. How to invert a tree on a whiteboard? Use a mirror.
评论 #9710599 未加载
评论 #9710639 未加载
cousin_it将近 10 年前
At first I thought that he was given a slightly harder problem: given a binary tree and a particular leaf node of that tree, return another tree that&#x27;s graph-isomorphic to the original, but has that node as the root. (You can visualize that as picking up the tree by a leaf node and shaking it, then reinterpreting the result as another tree.)
raverbashing将近 10 年前
Too bad the website requires you to sign up to run your solution.<p>But it&#x27;s an interesting concept, I&#x27;ve had to use a similar one in the past for a job interview.
评论 #9710647 未加载
评论 #9710623 未加载
djhworld将近 10 年前
I&#x27;ve enjoyed many of the responses on this topic, some people like to use it as a self congratulatory platform to express their position that the solution is trivial and &quot;any engineer should be able to do it like me&quot; with a piece of example code, while others use it as a soapbox to moan about the scourge of the tech interview process.
评论 #9710849 未加载
AYBABTME将近 10 年前
This topic has spilled too much ink.
评论 #9710852 未加载
评论 #9710588 未加载
ripitrust将近 10 年前
C++ Solution here :<p>TreeNode* invertTree(TreeNode* root) { if (root) { TreeNode* temp = root-&gt;left; root-&gt;left = invertTree(root-&gt;right); root-&gt;right = invertTree(temp); } return root; }
wfunction将近 10 年前
Considering the answer is 1 line, I doubt I would have hired him either...<p><pre><code> def inverse(t): return Node(inverse(t.r), t.v, inverse(t.l)) if t else t </code></pre> or if you must do it in place, 3 lines...<p><pre><code> def invert(t): if t: (t.l, t.r) = (invert(t.r), t.v, invert(t.l)) return t</code></pre>
ExpiredLink将近 10 年前
&gt;&gt; <i>You have not signed in, cannot submit your code.&lt;&#x2F;a&gt;</i>
评论 #9710621 未加载
adnanh将近 10 年前
My JS<p><pre><code> var invertTree = function(root) { if (!root) return null; root.right = [invertTree(root.left), root.left = invertTree(root.right)][0]; return root; };</code></pre>
jbrooksuk将近 10 年前
As a non-CS major, what is inverting a binary tree used for? Google just keeps bringing me back to Max&#x27;s tweet, or blog posts about it.
评论 #9710641 未加载
fishnchips将近 10 年前
This is just flogging a dead horse.