20 likes | 122 Views
Neither!. // Imbalance of a BST defined to be the absolute difference // between depth of its left and right subtrees. public static int imbalance( BSTnode bn ) { return Math.abs( Depth ( bn.left )- Depth ( bn.right )); }
E N D
// Imbalance of a BST defined to be the absolute difference// between depth of its left and right subtrees public static int imbalance(BSTnodebn) {return Math.abs(Depth(bn.left)-Depth(bn.right)); } public static int Depth(BSTnodebn) {if(bn == null) return 0;return Math.Max(Depth(bn.left), Depth(bn.right))+1; } What is the complexity of this algorithm? This algorithm visits every node once, so complexity is O(n)