simple java solution


  • 0
    2
    /**
     * 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 upsideDownBinaryTree(TreeNode root) {
            if (root == null) return root;
            Stack<TreeNode> s = new Stack();
            s.push(root);
            while (root.left != null) {
                root = root.left;
                s.push(root);
            }
            while (s.size() > 1) {
                TreeNode t = s.pop();
                TreeNode parent = s.peek();
                t.left = parent.right;
                t.right = parent;
                parent.left = null;
                parent.right = null;
            }
            return root;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.