zoukankan      html  css  js  c++  java
  • leecode 树的平衡判定 java

    以前写过c++版本的,感觉java写的好舒心啊
    /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isBalanced(TreeNode root) { if(root==null) return true; if(Math.abs(depth(root.left)-depth(root.right))<=1) { return isBalanced(root.left)&&isBalanced(root.right); } else { return false; } } public int depth(TreeNode tn) { if(tn==null) return 0; int le=depth(tn.left); int rl=depth(tn.right); if(le>=rl) return le+1; else return rl+1; } }

      

  • 相关阅读:
    第一次作业
    第0次作业—姚舜禹17-1
    第三周作业
    第二周作业
    第一周作业
    第零周作业
    第三周作业
    第二周作业
    第一周作业
    第0次作业
  • 原文地址:https://www.cnblogs.com/hansongjiang/p/3815169.html
Copyright © 2011-2022 走看看