给出一个二叉树: Binary tree of height 3
我想找到两个处于同一级别的节点之间的水平距离,同时计算之间不存在的节点,而不计算节点本身, 用
说 f
/ \
g h
/ \ / \
a d
在节点 a 和 d 之间的水平距离为2。
编辑:
请查看a到d之间的距离是在同一级别上计算的,不包括a或d的父节点或子节点,而仅包括同一级别上的缺失节点。因此,a与d之间的距离将为a>(x> y)> d,其中x和y中分别是节点g和h的丢失子节点。因此,不计算目标节点a和d的水平距离为2
答案 0 :(得分:0)
这样想:
a
/ \
b c
/ \ / \
e f g h
现在,您要确定同一级别的节点之间的水平距离。例如:f和g。 这是逐步的方法:
f
(起始节点)时,将counter设置为0。g
(结束节点),则停止遍历。 更新:
正如anand_v.singh所指出的那样,如果树不可能在所有级别上都被完全填充,那么它将产生错误的结果。
为克服此问题,将确定一个名为tree_height
的附加参数。假设树的高度为3,则该数组最多包含2个 tree_height -1元素,所有元素均初始化为不等于任何树节点值的值。
现在,您可以使用类似二进制堆的数组表示形式的方法,将节点值放置在数组中的相应索引处。然后按照上述步骤获得结果。
答案 1 :(得分:0)
这是一种可能不是最佳的内存解决方案。
欢迎提出建议/改进。
算法-
复杂度- 时间:O(N) 空格:O(N)
假设- BT中的每个节点都有唯一的值。
class Node {
Node() {
value = -1;
}
Node(int num) {
value = num;
}
int value;
Node left = null;
Node right = null;
}
声明一些必需的DS
static Queue<Node> queue = new LinkedList<Node>();
static ArrayList<Integer> list = new ArrayList<Integer>();
static Set<Integer> set = new HashSet<Integer>();
然后是三个功能
static void convertBTToArray() {
if (set.isEmpty())
return;
Node node = queue.poll();
if (node != null) {
list.add(node.value);
if (node.value != -1)
set.remove(node.value);
if (node.left != null) {
queue.add(node.left);
set.add(node.left.value);
} else
queue.add(new Node());
if (node.right != null) {
queue.add(node.right);
set.add(node.right.value);
} else
queue.add(new Node());
convertBTToArray();
} else
return;
}
static void init(Node root) {
// traverse in BFS fashion (level order) and convert to Array.
Node rootCopy = root;
if (rootCopy != null) {
queue.add(rootCopy);
set.add(rootCopy.value);
convertBTToArray();
}
}
// get distance between start and end values.
static int getHorizontalDistance(int startValue, int endValue) {
int startIndex = -1, endIndex = -1;
for (int i = 0; i < list.size(); i++) {
if (startIndex == -1)
startIndex = list.get(i) == startValue ? i : -1;
if (list.get(i) == endValue) {
endIndex = i;
break;
}
}
// check if both numbers are from same level else return -1
if (Math.floor(Math.log(startIndex + 1) / Math.log(2)) >= 0
&& Math.floor(Math.log(endIndex + 1) / Math.log(2)) >= 0)
return (endIndex - startIndex - 1);
else
return -1;
}
最后是主要方法
public static void main(String...s) {
// create your tree and enter elements here
// -1 indicates that distance cant be found and hence invalid input.
System.out.println("Dist(7,1): "+getHorizontalDistance(7, 1));
}
答案 2 :(得分:-1)
Here is the code:
/**
*
* @author satish hawalppagol
*
*/
public class BinaryTreeTest
{
public int findDistance(Node root, int n1, int n2)
{
int leftNodeToRootNode = Pathlength(root, n1, "leftNodeToRootNode") - 2;
int rightNodeToRootNode = Pathlength(root, n2,"rightNodeToRootNode") - 2;
int lcaData = findLCA(root, n1, n2).data; //LCA->Lowest Common Ancestor
int lcaDistance = Pathlength(root, lcaData,"lcaDistance") - 1;
return (leftNodeToRootNode + rightNodeToRootNode) - 2 * lcaDistance;
}
public int Pathlength(Node root, int n1,String callingFrom)
{
if (root != null)
{
int x = 0;
if("rightNodeToRootNode" == callingFrom)
{
if(root.left ==null && root.right ==null)
{
//do nothing
}
else if(root.left ==null || root.right ==null)
{
System.out.println("counting the position where the node is not
present is : " + root.data);
}
if ((root.data == n1) || (x = Pathlength(root.left,
n1,"rightNodeToRootNode")) > 0 || (x = Pathlength(root.right,
n1,"rightNodeToRootNode")) > 0)
{
return x + 1;
}
}
if("rightNodeToRootNode" != callingFrom )
{
if ((root.data == n1) || (x = Pathlength(root.left,
n1,"leftNodeToRootNode")) > 0 || (x = Pathlength(root.right,
n1,"leftNodeToRootNode")) > 0)
{
return x + 1;
}
}
return 0;
}
return 0;
}
public Node findLCA(Node root, int n1, int n2)
{
if (root != null)
{
if (root.data == n1 || root.data == n2)
{
return root;
}
Node left = findLCA(root.left, n1, n2);
Node right = findLCA(root.right, n1, n2);
if (left != null && right != null)
{
return root;
}
if (left != null)
{
return left;
}
if (right != null)
{
return right;
}
}
return null;
}
public static void main(String[] args) throws java.lang.Exception
{
Node root = new Node(5);
root.left = new Node(2);
root.right = new Node(3);
/*root.left.right = new Node(12);*/
root.left.left = new Node(7);
root.left.left.left = new Node(9);
/*root.left.left.right = new Node(17);*/
root.right.right = new Node(1);
/*root.right.right.left = new Node(4);*/
root.right.right.right = new Node(6);
BinaryTreeTest binaryTreeTest = new BinaryTreeTest();
System.out.println("Distance between 9 and 6 is : " +
binaryTreeTest.findDistance(root,9, 6));
}
}
class Node
{
int data;
Node left;
Node right;
public Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
///////////input/////////
// 5 //
// / \ //
// 2 3 //
// / \ \ //
// 7 1 //
// / \ / \ //
// 9 6 //
///////////input/////////
counting the position where the node is not present is : 2
counting the position where the node is not present is : 7
counting the position where the node is not present is : 3
counting the position where the node is not present is : 1
Distance between 9 and 6 is : 4