编程之美3.8 求二叉树中节点的最大距离

题目:

如果把二叉树看成一个图,父子节点之间的连线看成是双向的,定义“距离”为两个节点之间边的个数。

求一颗二叉树中相距最远的两个节点之间的距离

分析:

二叉树中距离最远距离的情况出现在: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());
				}
				
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值