zoukankan      html  css  js  c++  java
  • 156. Binary Tree Upside Down

    题目:

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.

    For example:
    Given a binary tree {1,2,3,4,5},

        1
       / 
      2   3
     / 
    4   5
    

    return the root of the binary tree [4,5,2,#,#,3,1].

       4
      / 
     5   2
        / 
       3   1  
    

    链接: http://leetcode.com/problems/binary-tree-upside-down/

    题解:

    翻转二叉树。可以有recursive以及iterative两种方法。 Recursive是自底向上构建,很巧妙。 Iterative写得有点绕,二刷要好好再写一下。对于每个节点,要先保存这个节点,然后再进行修改,弄清楚reference就好写很多。

    Recursive, Time Complexity - O(n), Space Complexity - O(logn)

    /**
     * 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 || (root.left == null && root.right == null))
                return root;
            
            TreeNode newRoot = upsideDownBinaryTree(root.left);
            
            root.left.left = root.right;
            root.left.right = root;
            
            root.left = null;
            root.right = null;
            
            return newRoot;
        }
    }

    Iterative, Time Complexity - O(n), Space Complexity - O(1)

    /**
     * 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 || (root.left == null && root.right == null))
                return root;
            TreeNode newLeft = null, newRight = null, oldRight = root.right, newRoot = root.left;
            TreeNode prev = new TreeNode(root.val);
            
            while(newRoot != null) {
                newLeft = newRoot.left;
                newRight = newRoot.right;
                newRoot.left = oldRight;
                newRoot.right = prev;
                prev = newRoot;
                newRoot = newLeft;
                oldRight = newRight;
            }
            
            return prev;
        }
    }

    更简练的Iterative写法

    /**
     * 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 || (root.left == null && root.right == null))
                return root;
            TreeNode newRoot = root, prev = null, left = null, right = null;
    
            while(newRoot != null) {
                left = newRoot.left;
                newRoot.left = right;
                right = newRoot.right;
                newRoot.right = prev;
                prev = newRoot;
                newRoot = left;
            }
            
            return prev;
        }
    }

    二刷:

    Java:

    Recursive:

    我们要递归返回新的root,新的root是原二叉树最左端节点。sibling变为新树左节点,原root变为新树右节点。

    /**
     * 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 || root.left == null) return root;
            TreeNode newRoot = upsideDownBinaryTree(root.left);
            root.left.left = root.right;
            root.left.right = root;
            root.left = null;
            root.right = null;
            return newRoot;
        }
    }

    Iterative1:

    /**
     * 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;
            TreeNode left = root.left, right = root.right, newLeft = null, newRight = null;
            root.left = null;
            root.right = null;
            while (left != null) {
                newLeft = left.left;
                newRight = left.right;
                left.left = right;
                left.right = root;
                root = left;
                left = newLeft;
                right = newRight;
            }
            return root;
        }
    }

    Iterative2:

    /**
     * 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;
            TreeNode left = null, right = null, prev = null;
            while (root != null) {
                left = root.left;
                root.left = right;
                right = root.right;
                root.right = prev;
                prev = root;
                root = left;
            }
            return prev;
        }
    }

    Reference:

    https://leetcode.com/discuss/44718/clean-java-solution

    https://leetcode.com/discuss/67458/simple-java-recursive-method-use-one-auxiliary-node 

    https://leetcode.com/discuss/18410/easy-o-n-iteration-solution-java

  • 相关阅读:
    Nginx 静态资源缓存设置
    Ubuntu Linux 与 Windows 7双系统安装教程(图文)
    配置可以通过http协议访问的svn服务器
    CentOS下搭建SVN服务器
    LINUX服务器下用root登录ftp
    CentOS 6下编译安装MySQL 5.6
    Jenkins代码管理
    python学习:备份文档并压缩为zip格式
    centos 7 双网卡建网桥脚本实现
    python学习:使用正则收集ip信息
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4489708.html
Copyright © 2011-2022 走看看