algorithm - How to find and return bottom-most(Deepest Node) node of a binary tree? binary search tree? -



algorithm - How to find and return bottom-most(Deepest Node) node of a binary tree? binary search tree? -

i have binary tree status there single node in deepest level. nodes in tree have parent property (as left, right, data)

is possible determine node @ deepest level in improve o(n)? if tree binary search tree (right->data > parent->data, left->data < parent->data)instead of binary tree?

i can there using breadth-first approach gets job done in o(n) both binary tree , binary search tree wanted know if there improve approach.

even balanced tree you'll have check every sub-tree find deepest node, like:

struct result{ node *node; int level; }; result getdeepest( node *root ){ if( root == null ){ result result = {null, 0}; homecoming result; } result lresult = getdeepest( root->left ); result rresult = getdeepest( root->right ); result result = lresult.level < rresult.level ? rresult : lresult; ++ result.level; if( result.node == null ) result.node = root; homecoming result; }

algorithm tree binary-tree binary-search-tree

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -