• 二叉树和递归的巩固


    版权声明:

        本文由Faye_Zuo发布于http://www.cnblogs.com/zuofeiyi/, 本文可以被全部的转载或者部分使用,但请注明出处.

      我回到Java的学习里面来了。看到之前学习的代码,完全是恍如隔世,感觉一辈子也不想再碰程序。但是想着29岁的自己,也还年轻,所以必须坚持和坚强。准备重新把之前的程序写一遍,我经历着一个人学习,一个人转行奋斗的孤独感。于是拼命想在图书馆找到学习的同伴,期待可以建一个转行群,共同学习,互相勉励。努力搭讪后,发现了两三个文科转CS的同伴,聊天加微信后,最后发现这种方法对我来讲其实还挺浪费时间的。因为大家方法都不一样,目标也不一样,这样反而把时间浪费在无聊的聊天中。对于我来讲,还是适合找牛一点的人直接指点迷津。于是我决定成为一个转行CS的独行侠!

      我花了大概十天时间把之前的程序又写了一遍,很枯燥。但是在这个过程中,自己对程序的理解好像又加深了那么一点点,特别是对面向对象的理解。之前对class、method、构造函数什么的觉得非常浆糊,现在说不清道不明的变得逐渐清晰起来了。看来学习程序就得写代码,突破不了这个心理障碍真的就学不动了。

      然后讲讲这周的学习进度。这周我主要练习了leetcode上的三道题目,通过这三道题目的掌握,希望可以对二叉树(Binary Tree)和递归(recursion)有更加深入的了解。

    一、Maximum Depth of Binary Tree(104)

    Given a binary Tree,find its maximum depth.

    The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 
     
    我的思路:运用递归。1.用递归的方法分别求得左子树和右子树的深度;2.写一个函数来比较两个子树的大小;3.返回大的子树 
     
    经过调试,完整程序如下:
     
    public class Solution {
        public int max(int a,int b){
            if(a>b){
                return a;
            }
            else{
                return b;
            }
        }
     
        public int maxDepth(TreeNode root) {
        int count=1;
        if(root==null){
            return 0;
        }
        if(root.left!=0||root.right!=0){
           int leftMax=maxDepth(root.left);
           int rightMax=maxDepth(root.right);
           count=max(leftMax,rightMax)+1;
        }
        return count;
        }
    }
     
    在以上程序的编写过程中,经历了各种千疮百孔,感觉又无数的红叉叉。后来发现是静态变量(static)始终没有搞懂。那么到底什么情况下需要加static?
    1. 访问内部类的时候
    2. 在一个类里面访问另一个类的时候
    3. 访问一个类里面的特有数据时。
     
    二、Same Tree(100)
    Given two binary trees,write a function to check if they are equal or not.
    Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
    public class TheSameTree {

      public static void main(String[] args) {
        TreeNode root1=new TreeNode(1);
        root1.left=new TreeNode(2);
        root1.right=new TreeNode(3);
        root1.left.left=new TreeNode(8);
       
        TreeNode root2=new TreeNode(1);
        root2.left=new TreeNode(2);
        root2.right=new TreeNode(3);
        root2.left.left=new TreeNode(8);
       
        Solution A= new Solution();
        System.out.print(A.isSameTree(root1, root2));
       

      }

      public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
          val = x;
        }
      }

      public static class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            if(p==null&&q==null){
              return true;
            }
            if(p==null||q==null){
              return false;
            }
            if(p.val!=q.val){
              return false;
            }
          
            return  isSameTree(p.left,q.left)&&isSameTree(p.right,q.right); 
        }
      }
    }
     
    这道题让我加深了对递归的理解(感觉其实还有很多没有掌握)
    1.递归调用一定注意两个部分:if 和 return
    2.一定是一层一层调用,再一层一层返回。 
     
    三、Balanced Binary Tree(110)
    Given a binary tree,determine if it is height-balanced.
    For this problem,a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 
     
    我的思路:1.先求出左子树的最大深度;2.再求出右子树的最大深度;3.再求出他们的绝对值小于1;
     
    public static class Solution {
        public boolean isBalanced(TreeNode root) {
          int value=0;
          if(root==null){
            return true;
          }
          else{
            int leftMaxNew=maxDepth(root.left);
            int rightMaxNew=maxDepth(root.right);
             value=leftMaxNew-rightMaxNew;
             if((value<1)||(value>1)){
               return false;
             }
          }
          return isBalanced(root.left)&&isBalanced(root.right);
        }
     
      这就是本周我所做的三道题和一些简单感悟。这次做题最大的进步在于三道题都有自己的思路,不再是去背别人的答案,而是基本自己写出来的。虽然看上去可能很啰嗦,但是自己有进步就很开心。下周开始学习深度优先或者广度优先。加油!
     
     
     
     
     
     
  • 相关阅读:
    算法53----换钱的最小次数和方法数【动态规划】
    算法52-----矩阵最小路径【动态规划】
    Data
    Git
    Git
    Git
    Git
    DevOps
    Docker
    Tools
  • 原文地址:https://www.cnblogs.com/zuofeiyi/p/6346393.html
Copyright © 2020-2023  润新知