面试算法——权值最大的叶节点到权值最小的叶节点的距离

有一棵二叉树,树上每个点标有权值,权值各不相同,请设计一个算法算出权值最大的叶节点到权值最小的叶节点的距离。二叉树每条边的距离为1,一个节点经过多少条边到达另一个节点为这两个节点之间的距离。

给定二叉树的根节点root,请返回所求距离。

  • 复习一下关于树的相关知识,深度优先遍历(DFS)和广度优先遍历(BFS)等
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class MaxMinTree {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MaxMinTree tree = new MaxMinTree();
		TreeNode root = new TreeNode(5);
		TreeNode left = new TreeNode(4);
		TreeNode right = new TreeNode(6);
		root.left = left;
		root.right = right;
		TreeNode r2 = new TreeNode(8);
		right.right = r2;
		Map map=new HashMap<>();
		
		System.out.println(tree.getDis(root));
		
		//dfs
		/*tree.dfs(root, map, 1);
		System.out.println(tree.dpest);
		for(int i=0;i map){
		while(map.containsKey(node)){
			System.out.print(node.val+"<--");
			node = map.get(node);
		}
		System.out.println(node.val);
	}
	public int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;

	public int getDis(TreeNode root) {
		HashMap map = new HashMap<>();
		helper(root, map);
		// showMap(map);
		System.out.println("min:" + min + ",max" + max);
		// root.left=null;
		List path1 = getPath(min, map, new ArrayList());
		List path2 = getPath(max, map, new ArrayList());

		int len1 = path1.size();
		int len2 = path2.size();
		int result = 0;
		for (int i = len1 - 1, j = len2 - 1; i >= 0 && j >= 0; i--, j--) {
			if (path1.get(i) == path2.get(j)) {
				result = i + j;
			}
		}
		return result;

	}

	private List getPath(int i, Map map, List list) {
		while (map.containsKey(i)) {
			list.add(i);
			i = map.get(i);
		}
		list.add(i);
		return list;
	}

	void showMap(Map map) {
		Iterator iterator = map.keySet().iterator();
		while (iterator.hasNext()) {
			Integer i = iterator.next();
			System.out.println("key:" + i + ",value:" + map.get(i));
		}
	}

	int dpest = 1;
	List leaves=new ArrayList<>();

	public void dfs(TreeNode root, Map map, int dp) {
		if (root == null)
			return;
		if (root.left == null && root.right == null) {
			//map.put(root, null);
			dpest = dpest < dp ? dp : dpest;
			leaves.add(root);
		}
		if (root.left != null) {
			map.put(root.left, root);
			dfs(root.left, map, dp + 1);
		}
		if (root.right != null) {
			map.put(root.right, root);
			dfs(root.right, map, dp + 1);
		}
	}
	
	public void bfs(TreeNode root){
		LinkedList queue = new LinkedList<>();
		queue.add(root);
		while(!queue.isEmpty()){			
			TreeNode tmp=queue.poll();//queue remove
			System.out.print(tmp.val+" ");
			//while()
			if(tmp.left!=null){
				queue.add(tmp.left);//queue add
			}
			if(tmp.right!=null){
				queue.add(tmp.right);
			}
		}
	}

	public void helper(TreeNode root, HashMap map) {
		if (root == null) {
			return;
		}
		if (root.left == null && root.right == null) {
			int val = root.val;
			min = val < min ? val : min;
			max = val > max ? val : max;
		}
		if (root.left != null) {
			map.put(root.left.val, root.val);
			helper(root.left, map);
		}
		if (root.right != null) {
			map.put(root.right.val, root.val);
			helper(root.right, map);
		}
	}

}


你可能感兴趣的:(面试算法)