构建二叉树 -- 父节点与左右子节点 最近公共祖先

在二叉树中找到两个节点的最近公共祖先

http://www.nowcoder.com/questionTerminal/c75deef6d4bf40249c785f240dad4247

import java.util.*;
public class Main {
    static HashMap hm;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] str = sc.nextLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int r = Integer.parseInt(str[1]);

        // 读取数据存入HashMa<Integer,Integer[]>中
        Integer[] child;
        hm = new HashMap();
        for(int i = 0;i < n;i++){
            String[] str1 = sc.nextLine().split(" ");
            int fa = Integer.parseInt(str1[0]);
            int lc = Integer.parseInt(str1[1]);
            int rc = Integer.parseInt(str1[2]);
            child = new Integer[2];
            child[0] = lc;
            child[1] = rc;
            hm.put(fa, child);
        }

        String[] str2 = sc.nextLine().split(" ");
        int o1 = Integer.parseInt(str2[0]);
        int o2 = Integer.parseInt(str2[1]);

        Node root = new Node(r);
        Node node1 = new Node(o1);
        Node node2 = new Node(o2);
        buildTree(root);
        Node res = LCA(root, node1, node2);
        System.out.println(res.val);
    }

    // 递归查找最近公共祖先
    private static Node LCA(Node root, Node node1, Node node2){
        if(root == null){
            return null;
        }
        // 查找到对应点即返回该点Node
        if(root.val == node1.val){
            return node1;
        }
        if(root.val == node2.val){
            return node2;
        }
        Node left = LCA(root.left, node1,node2);
        Node right = LCA(root.right, node1,node2);
        if(left != null && right != null){
            return root;
        }else if(left == null){
            return right;
        }else{
            return left;
        }
    }
    private static void buildTree(Node root) {

        if(root == null)
            return;

        if(hm.containsKey(root.val)){
            Node lc = null;
            Node rc =null;
            Integer[] ch = hm.get(root.val);
            if(ch[0]!= 0){
                lc = new Node(ch[0]);
                lc.parent = root;
            }
            if(ch[1]!= 0){
                rc = new Node(ch[1]);
                rc.parent = root;
            }
            root.left = lc;
            root.right = rc;

            buildTree(lc);
            buildTree(rc);
        }
    }
}
class Node {
    int val;
    Node parent;
    Node left;
    Node right;
    public Node(int val){
        this.val = val;
    }
}
全部评论

相关推荐

06-25 21:00
门头沟学院 Java
多拆解背记一下当前的高频场景面试题,结合自己的项目经历去作答,面试通过率原来真的不会低!
牛客96559368...:小公司不就是这样的吗,面试要么是点击就送,要么就是往死里拷打,没有一个统一的标准。这个不能代表所有公司
点赞 评论 收藏
分享
真烦好烦真烦:豆包润色了自己没看看吗,再说了,都说豆包是愚蠢且勤快的大学生,ds才是聪明的研究生,怎么敢让豆包写论文的
你们的毕业论文什么进度了
点赞 评论 收藏
分享
程序员牛肉:主要是因为小厂的资金本来就很吃紧,所以更喜欢有实习经历的同学。来了就能上手。 而大厂因为钱多,实习生一天三四百的就不算事。所以愿意培养你,在面试的时候也就不在乎你有没有实习(除非是同级别大厂的实习。) 按照你的简历来看,同质化太严重了。项目也很烂大街。 要么换项目,要么考研。 你现在选择工作的话,前景不是很好了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务