39、平衡二叉树
一、題目
輸入一棵二叉樹,判斷該二叉樹是否是平衡二叉樹。
二、解法
1 public class Solution { 2 private boolean isBalanced = true; 3 public boolean IsBalanced_Solution(TreeNode root) { 4 //后續遍歷時,遍歷到一個節點,其左右子樹已經遍歷 依次自底向上判斷,每個節點只需要遍歷一次 5 getDepth(root); 6 return isBalanced; 7 } 8 public int getDepth(TreeNode root){ 9 if(root == null) 10 return 0; 11 int left = getDepth(root.left); 12 int right = getDepth(root.right); 13 if(Math.abs(left - right) > 1) 14 isBalanced = false; 15 return right>left ? right+1 : left+1; 16 } 17 }?
轉載于:https://www.cnblogs.com/fankongkong/p/7456662.html
總結
- 上一篇: jmeter--轻量级接口自动化测试框架
- 下一篇: Go 1.9 sync.Map揭秘