https://oj.leetcode.com/problems/binary-tree-upside-down/
http://blog.csdn.net/xudli/article/details/42362441
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode UpsideDownBinaryTree(TreeNode root) { if (root == null) return null; Stack<TreeNode> stack = new Stack<>(); TreeNode node = root; while (node != null) { stack.push(node); node = node.left; } TreeNode newhead = null; while (!stack.empty()) { node = stack.pop(); if (newhead == null) newhead = node; if (!stack.empty()) { node.left = stack.peek().right; node.right = stack.peek(); } else { node.left = null; node.right = null; } } return newhead; } }