程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> Tree_Graph 判斷是否平衡二叉樹 @CareerCup

Tree_Graph 判斷是否平衡二叉樹 @CareerCup

編輯:關於PHP編程

Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.


平衡二叉樹的定義為:它是一棵空樹或它的左右兩個子樹的高度差的絕對值不超過1, 並且左右兩個子樹都是一棵平衡二叉樹。


思路:

1)先寫一個遞歸的樹的高度函數,然後檢查子樹的高度差是否大於1

2)優化:把檢查子樹高度差是否大於1的邏輯放在求樹的高度的遞歸函數中,並且遇到非平衡就及時返回。


注:

這道題不同於問一棵樹是否平衡(這棵樹任意兩個葉子結點到根結點的距離之差不大於1):

\
<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPsjnyc/NvKOszqrGvbritv6y5sr3o6y1q7K7xr264qGjPC9wPgo8cD7F0LbP0ru/w8r3yse38ca9uuK/ydLUx/PK97XE1+6087jftsi6zdfu0KG437bI1q6y7srHt/G089PaMaGjPC9wPgo8cD7H88r3tcTX7tChuN+2yL/Jss6/vKO6aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmlnaHRmb3J5b3VyZHJlYW0vYXJ0aWNsZS9kZXRhaWxzLzEyODUxMjMxPC9wPgo8cD48YnI+CjwvcD4KPHA+we3Su9bWveK3qMrHv8nS1NPD1tDQ8rHpwPrH87XDyvfA77XEw7/Su7j20rbX07XEuN+2yKOsyLu687/JtcOhozwvcD4KPHA+ss6/vKO6aHR0cDovL2hhd3N0ZWluLmNvbS9wb3N0cy80LjEuaHRtbDwvcD4KPHA+PGJyPgo8L3A+CjxwPs/Cw+bKx8XQts/Kx7fxxr264rb+subK97XEtPrC66O6PC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;">package Tree_Graph; import CtCILibrary.AssortedMethods; import CtCILibrary.TreeNode; public class S4_1 { // 遞歸判斷樹是否平衡二叉樹 // Time: O(N^2) public static boolean isBalanced(TreeNode root) { if (root == null) { return true; } int heightDiff = getHeight(root.left) - getHeight(root.right); if(Math.abs(heightDiff) > 1) { // 非平衡 return false; } else { return isBalanced(root.left) && isBalanced(root.right); } } // 遞歸獲得樹的高度 public static int getHeight(TreeNode root) { if (root == null) { return 0; } return Math.max(getHeight(root.left), getHeight(root.right)) + 1; } // ========================== Improved version 優化版 // 把判斷是否平衡的邏輯放在checkHeight函數裡,邊計算高度, // 邊判斷是否平衡,如果不平衡,直接返回-1 // Time: O(N), Space: O(H), H: height of tree public static boolean isBalanced2 (TreeNode root) { if (checkHeight(root) == -1) { return false; } else{ return true; } } // 邊計算高度邊判斷是否平衡 public static int checkHeight (TreeNode root) { if (root == null) { return 0; } int leftHeight = checkHeight(root.left); if (leftHeight == -1) { return -1; } int rightHeight = checkHeight(root.right); if (rightHeight == -1) { return -1; } int heightDiff = leftHeight - rightHeight; if (Math.abs(heightDiff) > 1) { return -1; } return Math.max(leftHeight, rightHeight) + 1; } public static void main(String[] args) { // Create balanced tree int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; TreeNode root = TreeNode.createMinimalBST(array); System.out.println("Root? " + root.data); System.out.println("Is balanced? " + isBalanced(root)); // Could be balanced, actually, but it's very unlikely... TreeNode unbalanced = new TreeNode(10); for (int i = 0; i < 10; i++) { unbalanced.insertInOrder(AssortedMethods.randomIntInRange(0, 100)); } System.out.println("Root? " + unbalanced.data); System.out.println("Is balanced? " + isBalanced(unbalanced)); } }



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved