Created
July 21, 2015 09:12
-
-
Save fever324/0abf6a404a617782f980 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for a binary tree node. | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; | |
| * TreeNode(int x) { val = x; } | |
| * } | |
| */ | |
| public class Solution { | |
| public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | |
| if ((root.val >= p.val && root.val <= q.val) || (root.val <= p.val && root.val >= q.val)) { | |
| return root; | |
| } else { | |
| if (root.val > p.val) { | |
| return lowestCommonAncestor(root.left, p, q); | |
| } else { | |
| return lowestCommonAncestor(root.right, p ,q); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment