求二叉树中节点的最大距离递归解法

问题定义

如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义"距离"为两节点之间边的个数。写一个程序求一棵二叉树中相距最远的两个节点之间的距离。

代码实现中二叉树用的是完全二叉树

import java.util.LinkedList;


public class Test_3_8 {

	public static int maxlen=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;
		//如果左子树为空,那么该节点的左边最长距离为0
		if(root.getLeftchild()==null)
			root.setMaxleft(0);
		//如果右子树为空,那么该节点的右边最长距离为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.getRightchild().getMaxright();
			root.setMaxright(tempmax+1);
		}
		//更新最长距离
		if(root.getMaxleft()+root.getMaxright()>maxlen){
			maxlen=root.getMaxleft()+root.getMaxright();
		}
	}

}
class TreeNode{
	private TreeNode leftchild;//左子树
	private TreeNode rightchild;//右子树
	private int maxleft;//左子树中的最长距离
	private int maxright;//右子树中的最长距离
	private int value;//该节点的值
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public TreeNode getLeftchild() {
		return leftchild;
	}
	public void setLeftchild(TreeNode leftchild) {
		this.leftchild = leftchild;
	}
	public TreeNode getRightchild() {
		return rightchild;
	}
	public void setRightchild(TreeNode rightchild) {
		this.rightchild = rightchild;
	}
	public int getMaxleft() {
		return maxleft;
	}
	public void setMaxleft(int maxleft) {
		this.maxleft = maxleft;
	}
	public int getMaxright() {
		return maxright;
	}
	public void setMaxright(int maxright) {
		this.maxright = maxright;
	}
	
}
class Tree{
	private TreeNode root;

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}
	
}
class Queue1{
	private LinkedList<TreeNode> list;
	public Queue1(){
		this.list=new LinkedList<TreeNode>();
	}
	public void push(TreeNode node){
		list.add(node);
	}
	public TreeNode pop(){
		return list.removeFirst();
	}
	public boolean isEmpty(){
		return list.isEmpty();
	}
}
class BTree{
	private Tree tree;
	public Tree getTree() {
		return tree;
	}
	public void setTree(Tree tree) {
		this.tree = tree;
	}
	private Queue1 queue;
	public BTree(){
		this.tree=new Tree();
	}
	public void insertnode(TreeNode node){
		if(tree.getRoot()==null){
			tree.setRoot(node);
			return;
		}
		else{
			Queue1 queue=new Queue1();
			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());
				}
			}
		}
	}
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章