题目:
如果把二叉树看成一个图,父子节点之间的连线看成是双向的,定义“距离”为两个节点之间边的个数。
求一颗二叉树中相距最远的两个节点之间的距离
分析:
二叉树中距离最远距离的情况出现在:1.根节点到叶子节点。2.叶子节点到另一叶子节点
代码如下:
public class TreeDistance
{
public static int manlen=0;
public static void main(String[] args)
{
BTree btree = new BTree();
int[] arr={1,2,3,4,5};
for(int i =0;i<arr.length;i++)
{
TreeNode newnode = new TreeNode();
newnode.setValue(i);
newnode.setMaxLeft(0);
newnode.setMaxRight(0);
btree.insertNode(newnode);
}
FindMaxLen(btree.getTree().getRoot());
System.out.println(maxlen);
}
public static void FindMaxLen(TreeNode root)
{
if(root == null)
{
return;
}
if(root.getLeftChild()==null)
root.setMaxLeft(0);
if(root.getRightChild()==null)
root.setMaxRight(0);
if(root.getLeftChild()!=null)
FindMaxLen(root.getLeftChild());
if(root.getRightChild()!=null)
FindMaxLen(root.getRightChild());
if(root.getLeftChild()!=null)
{
int tempMax = 0;
if(root.getLeftChild().getMaxLeft()>root.getLeftChild.getMaxRight())
tempMax = root.getLeftChild().getMaxLeft():
else
tempMax = root.getLeftChild().getMaxRight();
root.setMaxLeft(tempMax+1);
}
if(root.getRightChild()!=null)
{
int tempMax = 0;
if(root.getRightChild().getMaxLeft()>root.getRightChild.getMaxRight())
tempMax = root.getRightChild().getMaxLeft():
else
tempMax = root.getRigthChild().getMaxRight();
root.setMaxRight(tempMax+1);
}
if((root.getMaxLeft()+root.getMaxRight())>maxLen)
maxLen = root.getMaxLeft()+root.getMaxRight();
}
}
class TreeNode
{
String data;
TreeNode leftChild;
TreeNode rightChild;
int maxLeft;
int maxRight;
public TreeNode(String data)
{
this.data = data;
}
public void setMaxLeft(int max)
{
maxLeft = max;
}
public void setMaxRight(int max)
{
maxRight = max;
}
public int getMaxLeft()
{
return maxLeft;
}
public int getMaxRight()
{
return maxRight;
}
public void setLeftChild(TreeNode leftChild)
{
this.leftChild = leftChild;
}
public void setRightChild(TreeNode rightChild)
{
this.rightChild = rightChild;
}
public TreeNode getLeftChild()
{
return leftChild;
}
public TreeNode getRightChild()
{
return rightChild;
}
}
class Tree
{
private TreeNode root;
public TreeNode getRoot()
{
return root;
}
public void setRoot(TreeNode root)
{
this.root = root;
}
}
class Queue
{
private LinkedList<TreeNode> queue;
public Queue()
{
queue = new LinkedList<TreeNode>();
}
public void push(TreeNode newNode)
{
queue.add(newNode);
}
public TreeNode pop()
{
return queue.removeFirst();
}
public boolean isEmpty()
{
return queue.size()==0;
}
}
class BTree
{
private Tree tree;
public Tree getTree()
{
return tree;
}
public void setTree(Tree tree)
{
this.tree = tree;
}
private Queue queue;
public BTree()
{
this.tree = new Tree();
}
public void insertNode(TreeNode node)
{
if(tree.getRoot==null)
{
tree.setRoot(node);
return;
}
else
{
queue = new Queue();
queue.push(tree.getRoot());
while(!queue.isEmpty())
{
TreeNode temp = queue.pop();
if(temp.getLeftChild()==null)
{
temp.setLeftChild(node);
return;
}
else if(temp.getRightChild()==null)
{
temp.setRightChild(node);
return;
}
else
{
queue.push(temp.getLeftChild());
queue.push(temp.getRightChild());
}
}
}
}
}