Invert a binary tree on LeetCode
leetcode.com
leetcode.com
> @rogerdai16 to min-max the tree, ascending to descending.
One more for the toolbox, I guess.
def invertTree(self, root):
if root is not None:
tmp = root.left
root.left = self.invertTree(root.right)
root.right = self.invertTree(tmp)
return rootI think people were making fun of Google, because it is a caricature of their whole interview process.
If a large number of your engineers is using Homebrew, having its developer is an asset for many different reasons (e.g. influencing the focus of the project, getting formulae in that interest you, or simply improving a tool that is important to your productivity). But no, they reject him because he cannot invert a binary tree.
It's like throwing one of the better offenders out of your soccer team, because he cannot substitute as a goalkeeper.
In fact, I would see Google hiring the leaders of open source projects in order to influence the direction the project takes as a problem. Sponsoring an open source project is brilliant, and Google are great at that, but buying influence over it is completely different and (potentially) an issue that would lead to people forking the project.
While I understand that interviews are sometimes necessary to evaluate a candidate, it seems completely out of touch for Google to pass on an applicant with such an impressive background.
Not only that, but heaven knows that it takes a lot of dedication and leadership to lead an open source project from ideation to wide-spread use. If I were a startup founder, I would kill to get someone like him on my team.
To be honest, it kind of makes me wonder about the kind of code Google has, if this is a measure of a good coder. Are low level algorithms haphazardly re-implemented in python everywhere there is a need for one?
I've seen lots of code like that before. I was very unimpressed.
def invertTree(self, root):
queue = []
if root is not None:
queue.append(root)
while queue:
current = queue.pop()
current.left, current.right = current.right, current.left
if current.left is not None:
queue.append(current.left)
if current.right is not None:
queue.append(current.right)
return rootBut I guess it's file while ignoring the order of the items, and just caring if it's containing items or empty.
Carry on :)
(defun mirror (tree)
(when tree
(tree
(tree-data tree)
(mirror (tree-right tree))
(mirror (tree-left tree)))))
For completness, here are the definitions: (defpackage :trees (:use :cl)
(:shadow #:copy-tree))
(in-package :trees)
(defstruct (tree (:constructor tree (data &optional left right))
(:type list))
data left right)
As well as a test case: (mirror (tree 4
(tree 2
(tree 1)
(tree 3))
(tree 7
(tree 6)
(tree 9))))
=> (4 (7 (9 NIL NIL) (6 NIL NIL)) (2 (3 NIL NIL) (1 NIL NIL))) def invertTree(self, root):
queue = []
if root is not None:
queue.append(root)
while current is not None or len(queue) > 0:
while current is None:
current = queue.pop()
current.left, current.right = current.right, current.left
if current.left is not None:
if current.right is not None:
queue.append(current.right)
current = current.left
else:
current = current.right
return root
No needless enqueuing / dequeuing of things unless you need to. As a bonus, if the nodes maintain a count of how many children the have overall you can always recurse on the smaller child first and use less memory.var invertTree = function(root) {
if (!root) {
return root;
}
var left = root.left;
var right = root.right;
root.right = invertTree(left);
root.left = invertTree(right);
return root;
}; root.left, root.right = (self.invertTree(root.right),
self.invertTree(root.left))
(Parentheses added to break over two lines.) root.left, root.right = invert_tree(root.right),
invert_tree(root.left)Especially as Ruby has automatic unpacking of function returns, yes?
So something along the lines of:
a,b = function_returning_two_things(),
function_returning_one_thing()
could cause bugs.Can't off the top of my head recall actually seeing such a bug. I totally appreciate the desire to reduce the potential for errors from typos, but that ship has kind of already sailed with either language. I guess it just depends how far you're willing to let it go.
Yay choice. Testing will catch it anyway, right?
:/
<html>
<style>
pre {
-webkit-transform:scaleX(-1);
-moz-transform:scaleX(-1);
-ms-transform:scaleX(-1);
-o-transform:scaleX(-1);
transform:scaleX(-1);
}
</style>
<pre>
4
/ \
2 7
/ \ / \
1 3 6 9
</pre>
</html>But it's an interesting concept, I've had to use a similar one in the past for a job interview.
Of course, this creates its own issues related to creating and maintaining a mono-culture, but that's a topic for another thread.
TreeNode* invertTree(TreeNode* root) { if (root) { TreeNode* temp = root->left; root->left = invertTree(root->right); root->right = invertTree(temp); } return root; }
def inverse(t): return Node(inverse(t.r), t.v, inverse(t.l)) if t else t
or if you must do it in place, 3 lines... def invert(t):
if t: (t.l, t.r) = (invert(t.r), t.v, invert(t.l))
return t var invertTree = function(root) {
if (!root) return null;
root.right = [invertTree(root.left), root.left = invertTree(root.right)][0];
return root;
};