566 views
0 votes
0 votes
A node is height balanced if difference between height of left subtree and right subtree is either -1 , 0 or 1 , so how to count such nodes , without maintaining any extra field in the node except for left and right child pointers .

1 Answer

Best answer
3 votes
3 votes
Do a post order traversal of the tree.

First we traverse the left subtree - let it return the height of it - say n1.

Traverse the right subtree - let it return its height - say n2.

Now, if |n1 - n2| <= 1 increment a global counter.

Tree traversal is $O(n)$ and so we are done.
selected by

Related questions

1 votes
1 votes
1 answer
2
neha singh asked May 2, 2016
12,174 views
1 votes
1 votes
0 answers
3
Rohit Gupta 8 asked Dec 25, 2017
1,138 views
If the average depth of a node in an $n$-node binary search tree is $O(\log{n})$, then the height of the tree is$O(n\log{n})$$O(n)$$O(\log{n})$$O(\sqrt(n\log{n}))$
4 votes
4 votes
1 answer
4