• 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.

    Example:

    Input: [1,2,3,4,5]
    
        1
       / 
      2   3
     / 
    4   5
    
    Output: return the root of the binary tree [4,5,2,#,#,3,1]
    
       4
      / 
     5   2
        / 
       3   1  

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    二叉树中用left/right是recursive,类似图中的公式

    一个个节点去写是iterative,类似图中的for循环

    [一句话思路]:

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    六步里面:要提前把temp节点存起来,传递给cur.left

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    TreeNode类这样写 另外加一个item参数 相当于新建指针了
    然后solution里要包括一个root
    class TreeNode 
    {
      int data;
      TreeNode left, right;
      
      //parameter is another item
      TreeNode(int item) {
        data = item;
        left = right = null;
      }
    }

    新建节点需要tree.root = new,调用数据需要.data

    class MyCode {
      public static void main (String[] args) {
        Solution tree = new Solution();
        tree.root = new TreeNode(1);
        tree.root.left = new TreeNode(2);
        tree.root.right = new TreeNode(3);
        tree.root.left.left = new TreeNode(4);
        tree.root.left.right = new TreeNode(5);
    
        TreeNode t = tree.upsideDownBinaryTree(tree.root);
        System.out.println("t.root.data = " + t.data);
        System.out.println("t.root.left.data = " + t.left.data);
      }

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

    // package whatever; // don't place package name!
    
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    
    class TreeNode 
    {
      int data;
      TreeNode left, right;
      
      //parameter is another item
      TreeNode(int item) {
        data = item;
        left = right = null;
      }
    }
    
    
    class Solution {
      //new root
        TreeNode root;
      //method, need parameter, there is return 
        public TreeNode upsideDownBinaryTree(TreeNode root) {
            //ini: prev, cur, next;
            TreeNode cur = root;
            TreeNode prev = null;
            TreeNode temp = null;
            TreeNode next = null;
            
            //iteration
            while (cur != null) {
                next = cur.left;
                cur.left = temp;
                temp = cur.right;
                cur.right = prev;
                
                prev = cur;
                cur = next;
            }
            
            return prev;
        }
    }
    
    class MyCode {
      public static void main (String[] args) {
        Solution tree = new Solution();
        tree.root = new TreeNode(1);
        tree.root.left = new TreeNode(2);
        tree.root.right = new TreeNode(3);
        tree.root.left.left = new TreeNode(4);
        tree.root.left.right = new TreeNode(5);
    
        TreeNode t = tree.upsideDownBinaryTree(tree.root);
        System.out.println("t.root.data = " + t.data);
        System.out.println("t.root.left.data = " + t.left.data);
      }
    }
    View Code

     [潜台词] :

  • 相关阅读:
    luogu2253 好一个一中腰鼓!
    luogu2948 滑雪课
    luogu1556 幸福的路
    luogu1900 自我数
    luogu1632 点的移动
    luogu1999 高维正方体
    树状数组模板
    杜教筛
    [比赛|考试] 9月第一周的考试
    历年NOIP真题总结
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9376899.html
Copyright © 2020-2023  润新知