Leetcode 236. Lowest Common Ancestor of a Binary Tree

来源:互联网 发布:淘宝开店电脑端描述 编辑:程序博客网 时间:2024/05/17 02:20

236. Lowest Common Ancestor of a Binary Tree

Total Accepted: 62595 Total Submissions: 213961 Difficulty: Medium

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______3______       /              \    ___5__          ___1__   /      \        /      \   6      _2       0       8         /  \         7   4

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be 

a descendant of itself according to the LCA definition.

Hide Company Tags
 Amazon LinkedIn Apple Facebook Microsoft
Hide Tags
 Tree
Hide Similar Problems
 (E) Lowest Common Ancestor of a Binary Search Tree

思路:

最经典的common ancestor来了。

先是写了个235 Lowest Common Ancestor of a Binary Search Tree的dfs算法, check左右子树是否含有p, q。然后根据情况做判断。而且用的是boolean变量来保存结果,避免了在俩if中每个函数被多次调用,然而运行时间是 980ms..... what the hell ???? 多么美丽的递归代码啊!

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution { // 980 ms    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {        if(root == p || root == q) return root;        boolean leftp = dfs(root.left, p);        boolean leftq = dfs(root.left, q);        if(leftp && leftq) return lowestCommonAncestor(root.left, p, q);        else if(!leftp && !leftq) return lowestCommonAncestor(root.right, p, q);        else return root;    }        public boolean dfs(TreeNode root, TreeNode p){        if(root == null) return false;        if(root == p) return true;        if(dfs(root.left, p)) return true;        if(dfs(root.right, p)) return true;        return false;    }}

解法二:

来自这里。

分别在left和right子节点中调用本函数,得到一个TreeNode类型的返回值,记为left, right。(函数设定是只要碰到任何一个节点就返回)

1. 如果left, right均不为null,说明查找的俩节点分别位于两侧,返回root。

2. 如果left为null,说明right中存在查找的俩节点。由于我们从上而下,于是第一个碰到的节点一定是level最小的节点,返回right。

3. 如果right为null,情况和2对称,返回left。

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution { // 10ms    // 在root为根的二叉树中找A,B的LCA:    // 如果找到了就返回这个LCA    // 如果只碰到A,就返回A    // 如果只碰到B,就返回B    // 如果都没有,就返回null    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode node1, TreeNode node2) {        if (root == null || root == node1 || root == node2) {            return root;        }                // Divide        TreeNode left = lowestCommonAncestor(root.left, node1, node2);        TreeNode right = lowestCommonAncestor(root.right, node1, node2);                // Conquer        if (left != null && right != null) {            return root;        }         if (left != null) {            return left;        }        if (right != null) {            return right;        }        return null;    }}


0 0
原创粉丝点击