LeetCode-Lowest Common Ancestor of a Binary Tre

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.

Solution:

 1 /**
 2  * Definition for a binary tree node. public class TreeNode { int val; TreeNode
 3  * left; TreeNode right; TreeNode(int x) { val = x; } }
 4  */
 5 public class Solution {
 6     public class Result {
 7         boolean findP, findQ;
 8         TreeNode ancestor;
 9
10         public Result(boolean p, boolean q) {
11             findP = p;
12             findQ = q;
13             ancestor = null;
14         }
15     }
16
17     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
18         return findAncestorRecur(root, p, q).ancestor;
19     }
20
21     public Result findAncestorRecur(TreeNode cur, TreeNode p, TreeNode q) {
22         if (cur == null) {
23             return new Result(false, false);
24         }
25
26         boolean findP = (cur == p), findQ = (cur == q);
27         Result leftRes = findAncestorRecur(cur.left, p, q);
28         if (leftRes.ancestor != null)
29             return leftRes;
30         Result rightRes = findAncestorRecur(cur.right, p, q);
31         if (rightRes.ancestor != null)
32             return rightRes;
33
34         findP = (findP || leftRes.findP || rightRes.findP);
35         findQ = (findQ || leftRes.findQ || rightRes.findQ);
36
37         Result res = new Result(findP, findQ);
38         if (findP && findQ)
39             res.ancestor = cur;
40
41         return res;
42     }
43 }
时间: 2024-07-29 03:42:01

LeetCode-Lowest Common Ancestor of a Binary Tre的相关文章

leetcode - Lowest Common Ancestor of a Binary Search Tree

leetcode -  Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined

[LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

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

[LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. 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

LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告

https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between t

LeetCode——Lowest Common Ancestor of a Binary Search Tree

Description: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node i

LeetCode Lowest Common Ancestor of a Binary Serach Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. 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

leetcode——Lowest Common Ancestor of a Binary Tree

题目 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. 思路 这一次说的是一个普通的二叉树,给出两个节点,求他们的最低公共父节点. 回想一下,当这棵二叉树是二分查找树的时候的解决方案: 二分查找树解法:http://blog.csdn.net/langduhualangdu/article/details/47426339 没错,不管是二分查找树也好还是普通二叉树也

Leetcode ——Lowest Common Ancestor of a Binary Tree

Question 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 p and q as the lowest node in T that has bot

LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)

题意:给一棵二叉排序树,找p和q的LCA. 思路:给的是排序树,那么每个节点必定,大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 三种情况: (1)p和q都在root左边,那么往root左子树递归. (2)在右同理. (3)一左一右的,那么root->val肯定大于其中的1个,小于另一个. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNo

[LeetCode]Lowest Common Ancestor of a Binary Tree

第一个是普通二叉树,第二个是bst public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) { return null; } if (root == p || root == q) { return root; } TreeNode left = lowestCommonAncestor(root.left, p,