二叉树求最大最小权值问题的面试题

来源:互联网 发布:淘宝上300的狗能买吗 编辑:程序博客网 时间:2024/05/17 23:38

有一棵二叉树,树上每个点标有权值,权值各不相同,给定二叉树的根节点为root,请找出权值最大的节点与权值最小的节点; 假设父子节点距离为1,请计算权值最大节点与权值最小节点之间的距离(提示:可分别找出根节点到该两点间的路径,再减去两者从根节点开始的共同路径距离)
在很多面试中也有这样类似的问题,自己本身基础很弱,将学过的思路整理出来,方便自己以后的学习。话不多说,上思路。

思路:
先求最大最小叶子节点,再求两节点的LCA(最近公共祖先),求两节点到LCA距离。

首先得先明白如何求LCA(LCA属于比较经典的一类考点,定要掌握)。

概念,最近祖先节点: 最近祖先节点就是从根节点遍历到给定节点时的最后一个相同节点。

LCA思路:
如果给定Root是NULL,即空树,则返回的公共节点自然就是NULL;
情况一:如果左子树查找出的公共节点是NULL,则表明从左子树根节点开始到左子树的所有叶子节点等所有节点中,没有找到两个节点中的任何一个,这就说明,这两个节点不在左子树上,不在左子树,则必定在右子树上;
情况二:如果右子树查找的公共节点是NULL,说明在右子树中无法找到任何一个节点,则两个节点必定在左子树上;
情况三: 如果左右子树查找的公共节点都不是NULL,说明左右子树中各包含一个节点,则当前节点Root就是最低公共节点,返回就可以了。
public TreeNode GetLastCommonParent( TreeNode Root, TreeNode pNode1, TreeNode pNode2){
if( Root== null ) //说明是空树,不用查找了,也就找不到对应节点,则返回NULL
return null;
if( Root== pNode1 || pRoot == pNode2 )//说明在当前子树的根节点上找到两个节点之一
return Root;
TreeNode LeftNode = GetLastCommonParent( Root.left, pNode1, pNode2);//左子树中的查找两个节点并返回查找结果
TreeNode RightNode = GetLastCommonParent( Root.right, pNode1, pNode2);//右子树中查找两个节点并返回查找结果
if( LeftNode == null )//如果在左子树中没有找到,则断定两个节点都在右子树中,可以返回右子树中查询结果;否则,需要结合左右子树查询结果共同断定
return RightNode ;
if ( RightNode == null )//如果在右子树中没有找到,则断定两个节点都在左子树中,可以返回左子树中查询结果;否则,需要结合左右子树查询结果共同断定
return LeftNode ;
return Root;//如果在左右子树中都找两个节点之一,则Root就是最低公共祖先节点,返回即可。
}
如果给定Root与两个节点中任何一个相同,说明,Root在就是所要找的两个节点之一,则直接返回Root,表明在当前链路中找到至少一个节点;
如果给定Root不是两个节点中任何一个,则说明,需要在Root的左右子树中重新查找,此时有三种情况:两个节点都在左子树上;两个节点都在右子树上;一个在左子树,一个在右子树上;具体来说,就是:
三种情况是互斥的, 只能是其中之一。

Java实现:
先建立一个TreeNode类:

public class TreeNode {    int val = 0;    TreeNode left = null;    TreeNode right = null;    public TreeNode(int val){        this.val = val;    }}

然后在新建Tree中实现

public class Tree {    private TreeNode maxNode = new TreeNode(Integer.MIN_VALUE);    private TreeNode minNode = new TreeNode(Integer.MAX_VALUE);    public int getDis(TreeNode root){        getMaxMin(root);//找到最大最小叶子节点        TreeNode lcaNode = getLCA(root);//find LCA        int a = getNodeDis(lcaNode,maxNode);        int b = getNodeDis(lcaNode,minNode);        return a+b;    }    public void getMaxMin(TreeNode root) {        if(root == null){            return;        }        if(root.left == null && root.right == null){            if(root.val > maxNode.val){                maxNode = root;            }else if(root.val < minNode.val){                minNode = root;            }        }        getMaxMin(root.left);        getMaxMin(root.right);    }    private TreeNode getLCA(TreeNode root) {        if(root == null){            return null;        }        if(root.val == maxNode.val || root.val == minNode.val){            return root;        }        TreeNode leftNode = getLCA(root.left);        TreeNode rightNode = getLCA(root.right);        if(leftNode == null){            return rightNode;        }else if(rightNode == null){            return leftNode;        }else{            return root;        }    }    private int getNodeDis(TreeNode lcaNode, TreeNode node) {        if(lcaNode == null){            return -1;        }        if(lcaNode.val == node.val){            return 0;        }        int distance = getNodeDis(lcaNode.left,node);        if(distance == -1){            distance = getNodeDis(lcaNode.right,node);        }        if(distance !=-1){            return distance+1;        }        return -1;    }}
0 0
原创粉丝点击