LeetCode 236. Lowest Common Ancestor of a Binary Tree(浜屽弶鏍戞眰涓ょ偣LCA)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 236. Lowest Common Ancestor of a Binary Tree(浜屽弶鏍戞眰涓ょ偣LCA)相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/sof' title='sof'>sof str strong highlight code for val || int
棰樻剰锛?/strong>浜屽弶鏍戞眰涓ょ偣LCA銆?/span>
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root == NULL || root == p || root == q) return root; TreeNode* left = lowestCommonAncestor(root -> left, p, q); TreeNode* right = lowestCommonAncestor(root -> right, p, q); if(left && right) return root; return left ? left : right; } };
銆€銆€
以上是关于LeetCode 236. Lowest Common Ancestor of a Binary Tree(浜屽弶鏍戞眰涓ょ偣LCA)的主要内容,如果未能解决你的问题,请参考以下文章
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