Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Dr. Rick Sun code
- public TreeNode LCA(TreeNode root, TreeNode a, TreeNode b) {
- if (root == null ||root == a || root == b) return root;// base case 1; case 3
- TreeNode left = LCA(root.left, a, b);
- TreeNode right = LCA(root.right, a, b); // recursion step 1
- if (left != null && right != null) return root;// recursion step 2 + 3; case 1
- return (left != null ? left : right); // case 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement