Leetcode 236: Lowest Common Ancestor of a Binary Tree
Posted Keep walking
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 236: 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 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.
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public int val; 5 * public TreeNode left; 6 * public TreeNode right; 7 * public TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 private bool FindPath(TreeNode root, TreeNode node, IList<TreeNode> path) 12 { 13 var found = root == node || (root != null && (FindPath(root.left, node, path) || FindPath(root.right, node, path))); 14 if (found) path.Insert(0, root); 15 return found; 16 } 17 18 public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 19 IList<TreeNode> path1 = new List<TreeNode>(), path2 = new List<TreeNode>(); 20 FindPath(root, p, path1); 21 FindPath(root, q, path2); 22 23 int i = 0, len = Math.Min(path1.Count, path2.Count); 24 25 while (i < len && path1[i] == path2[i]) 26 { 27 i++; 28 } 29 30 return path1[i - 1]; 31 } 32 } 33 34 // the idea is right and used to work, however it seems Leetcode added a very large test case where this may cause stack overflow 35 public class Solution1 { 36 public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 37 if (root == null || root == p || root == q) return root; 38 39 var left = LowestCommonAncestor(root.left, p , q); 40 var right = LowestCommonAncestor(root.right, p , q); 41 42 if (left != null && right != null) 43 { 44 return root; 45 } 46 47 return left == null ? right : left; 48 } 49 } 50 51 // it work for any binary tree including BST 52 public class Solution2 { 53 private bool LCA(TreeNode root, TreeNode p, TreeNode q, bool[] found) 54 { 55 if (root == p) 56 { 57 found[0] = true; 58 } 59 60 if (root == q) 61 { 62 found[1] = true; 63 } 64 65 if (found[0] && found[1]) return true; 66 if (root == null) return false; 67 68 return LCA(root.left, p, q, found) || LCA(root.right, p, q, found); 69 } 70 71 public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 72 if (root == null) return null; 73 74 if (LCA(root.left, p, q, new bool[2])) 75 { 76 return LowestCommonAncestor(root.left, p, q); 77 } 78 else if (LCA(root.right, p, q, new bool[2])) 79 { 80 return LowestCommonAncestor(root.right, p, q); 81 } 82 83 return root; 84 } 85 }
以上是关于Leetcode 236: Lowest Common Ancestor of a Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode 236: Lowest Common Ancestor of a Binary Tree
leetcode 236: Lowest Common Ancestor of a Binary Tree
leetcode236 Lowest Common Ancestor of a Binary Tree
leetcode236 - Lowest Common Ancestor of a Binary Tree - medium