Sunday, January 24, 2016

Closest leaf to a given node in Binary Tree - GeeksforGeeks

Closest leaf to a given node in Binary Tree - GeeksforGeeks
Given a Binary Tree and a node x in it, find distance of the closest leaf to x in Binary Tree. If given node itself is a leaf, then distance is 0.

```Input: Root of below tree
And x = pointer to node 13
10
/     \
12       13
/     \
14       15
/   \     /  \
21   22   23   24
/\   /\   /\   /\
1 2  3 4  5 6  7  8

Output 2
Closest leaf is 12 through 10.
```
The idea is to first traverse the subtree rooted with give node and find the closest leaf in this subtree. Store this distance. Now traverse tree starting from root. If given node x is in left subtree of root, then find the closest leaf in right subtree, else find the closest left in left subtree.
Time Complexity of this above solution is O(n) as it does at most two traversals of given Binary Tree.

`class` `Distance `
`{`
`    ``int` `minDis = Integer.MAX_VALUE;`
`}`
`    ``// This function finds closest leaf to root.  This distance`
`    ``// is stored at *minDist.`
`    ``void` `findLeafDown(Node root, ``int` `lev, Distance minDist) `
`    ``{`
`         `
`        ``// base case`
`        ``if` `(root == ``null``) `
`            ``return``;`
` `
`        ``// If this is a leaf node, then check if it is closer`
`        ``// than the closest so far`
`        ``if` `(root.left == ``null` `&& root.right == ``null``) `
`        ``{`
`            ``if` `(lev < (minDist.minDis)) `
`                ``minDist.minDis = lev;`
`             `
`            ``return``;`
`        ``}`
` `
`        ``// Recur for left and right subtrees`
`        ``findLeafDown(root.left, lev + ``1``, minDist);`
`        ``findLeafDown(root.right, lev + ``1``, minDist);`
`    ``}`
` `
`    ``// This function finds if there is closer leaf to x through `
`    ``// parent node.`
`    ``int` `findThroughParent(Node root, Node x, Distance minDist) `
`    ``{`
`        ``// Base cases`
`        ``if` `(root == ``null``) `
`            ``return` `-``1``;`
`         `
`        ``if` `(root == x) `
`            ``return` `0``;`
`         `
`        ``// Search x in left subtree of root`
`        ``int` `l = findThroughParent(root.left, x, minDist);`
` `
`        ``// If left subtree has x`
`        ``if` `(l != -``1``) `
`        ``{    `
`            ``// Find closest leaf in right subtree`
`            ``findLeafDown(root.right, l + ``2``, minDist);`
`            ``return` `l + ``1``;`
`        ``}`
` `
`        ``// Search x in right subtree of root`
`        ``int` `r = findThroughParent(root.right, x, minDist);`
` `
`        ``// If right subtree has x`
`        ``if` `(r != -``1``) `
`        ``{`
`            ``// Find closest leaf in left subtree`
`            ``findLeafDown(root.left, r + ``2``, minDist);`
`            ``return` `r + ``1``;`
`        ``}`
` `
`        ``return` `-``1``;`
`    ``}`
` `
`    ``// Returns minimum distance of a leaf from given node x`
`    ``int` `minimumDistance(Node root, Node x) `
`    ``{`
`        ``// Initialize result (minimum distance from a leaf)`
`        ``Distance d = ``new` `Distance();`
` `
`        ``// Find closest leaf down to x`
`        ``findLeafDown(x, ``0``, d);`
` `
`        ``// See if there is a closer leaf through parent`
`        ``findThroughParent(root, x, d);`
` `
`        ``return` `d.minDis;`
`    ``}`

Related
http://massivealgorithms.blogspot.com/2016/11/binary-tree-all-paths-max-sum-path.html