zoukankan      html  css  js  c++  java
  • [LeetCode#156] Binary Tree Upside Down

    Problem:

    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  
    

    confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

    Wrong Solution:

    public class Solution {
        public TreeNode upsideDownBinaryTree(TreeNode root) {
            if (root == null)
                return root;
            TreeNode upsided_left = upsideDownBinaryTree(root.left);
            TreeNode upsided_right = upsideDownBinaryTree(root.right);
            if (upsided_left == null)
                return root;
            root.left = null;
            root.right = null;
            TreeNode right_most = upsided_left;
            if (right_most.right != null) 
                right_most = right_most.right;
            right_most.left = upsided_right;
            right_most.right = root;
            return upsided_left;
        }
    }

    Mistakes Analysis:

    Mistake analysis:
    Input:
    [1,2,null,3,null,4]
    Output:
    [4,null,3,null,1]
    Expected:
    [4,null,3,null,2,null,1]
    
    The above code is complicated and wrong. Cause I don't throughly understand the logic behind this problem. 
    I even try to find the right insert position at the upsided result, which is a indicator of wrong. 
    
    Actually the idea is really not hard.
    For a tree, we must sure,
    1. right child must be a leaf node or empty.
    2. the current left subtree would become the new root (see it as a single node).
    
    I understand the above two important points.
    But I miss this one.
    3. iff the left subtree is not a single node (the root of the left sub-tree would become its upsidedtree's rightmost node, where we should attach the root as left child and root as right child)
    Very important!!!!
    
    Thus above solution is complex and wrong.
    fix 1:  TreeNode upsided_right = upsideDownBinaryTree(root.right);
    Since right child must be a leaf or empty, there is no need to perform upsideDownBinaryTree over it.
    
    fix 2: Don't try to search to rightmost node manually, the left child of current tree has already been turned into the right most node.
    TreeNode right_most = upsided_left;
    if (right_most.right != null) 
        right_most = right_most.right;
    right_most.left = upsided_right;
    right_most.right = root;
    
    root pointer has not been updated after "upsideDownBinaryTree(root.left)", root.left still point to it's left child before upsided. (root's informaiton has not been changed yet!!!)
    root.left.left = root.right;
    root.left.right = root;

    You should also pay attention to the base case of this solution.
    It could be null pointer or leaf node
    null pointer : root == null
    leaf node : root.left == null && root.right == null
    In case we need to continue to search at next level when this only left child.

    Solution:

    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;
        }
    }
  • 相关阅读:
    分布式协调服务 ( 服务治理 ).
    【Java常用类之enumm枚举类 20】
    【Java常用类之date类 19】
    【Java常用类之String类18】
    【Java数组算法17】
    【Java数组-二维数组16】
    【Java数组-一维数组15】
    【Java异常 14】
    【Java的package/import&访问权限&修饰符&Object常用方法 13】
    《Rubu基础教程第五版》第十七章笔记 IO类
  • 原文地址:https://www.cnblogs.com/airwindow/p/4809040.html
Copyright © 2011-2022 走看看