public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { pst = new Stack<TreeNode>(); qst = new Stack<TreeNode>(); if (root == null) return null; TreeNode result, ptr = root; bool pb, qb; // common ance is that both p and q are child // lowest is that one node that both are true, but not for each of its child pb = isChildOf(ptr, p, pst); qb = isChildOf(ptr, q, qst); result = root; while(true){ if (pst.Count == 0 || qst.Count == 0 || pst.Peek() != qst.Peek()) return result; else { result = pst.Pop(); qst.Pop(); } } }
static void Main(string[] args) { TreeNode root = new TreeNode(5); TreeNode n1 = new TreeNode(3); TreeNode n2 = new TreeNode(6); root.left = n1; root.right = n2; TreeNode n3 = new TreeNode(1); TreeNode n4 = new TreeNode(4); TreeNode n7 = new TreeNode(2); n1.left = n3; n1.right = n4; n3.right = n7; Program p = new Program(); Console.WriteLine(p.LowestCommonAncestor(root, n3, n7).val); Console.ReadKey(); }
private bool isChildOf(TreeNode ance, TreeNode child, Stack<TreeNode> stack) { //bool result = false; if (ance == child) { //result = true; stack.Push(ance); return true; } if (ance.left != null && isChildOf(ance.left, child, stack)) { //result = true; stack.Push(ance); return true; } if (ance.right != null && isChildOf(ance.right, child, stack)) { //result = true; stack.Push(ance); return true; } return false; }