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

  • 相关阅读:
    澳洲中产收入水平[转]
    [转载]在澳洲做IT人士的收入差别
    取消excel 工作保护 密码的宏
    SAP ML 物料分类账详解(含取消激活物料帐方法)
    ABAP--如何创建自定义打印条码
    SAP 产品条码WMS结合 以及ABAP script的集成 BarCode
    SAP 以工序为基准进行发料 机加工行业 Goods Issue to Routing
    SAP HR模块的基础数据表和增强配置
    SAP财务供应链与金库管理的联系与区别
    评点SAP HR功能及人力资源管理软件
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4489708.html
Copyright © 2011-2022 走看看