Invert a binary tree.
4 / 2 7/\ /1 3 6 9
To
4 / 7 2/\ /9 6 3 1
Trivia:
This problem is inspired by this original tweets by Max Howell:
google:90% of our engineers with the software you wrote (Homebrew), but can ' t invert a binary tree on a Whitebo ard so fuck off.
296ms, but I think I still want to try other, for example, with a stack, the 2-fork tree First order to save, and then reverse the way to read the binary tree, but there is no warm use
/**
* 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) return null; if (root.left==null && root.right = = null) return root; TreeNode Lnode = Root.left; TreeNode Rnode = root.right; Root.right = Lnode; Inverttree (Root.right); Root.left = Rnode; Inverttree (Root.left); return root;} }
Leetcode-invert Binary Tree