求二叉樹中節點的最大距離遞歸解法

問題定義

如果我們把二叉樹看成一個圖,父子節點之間的連線看成是雙向的,我們姑且定義"距離"爲兩節點之間邊的個數。寫一個程序求一棵二叉樹中相距最遠的兩個節點之間的距離。

代碼實現中二叉樹用的是完全二叉樹

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());
				}
			}
		}
	}
}


 

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