HTMLify
lca_in_binarytree.java
Views: 1 | Author: cody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | // lca in binary tree gfg class Solution { //Function to return the lowest common ancestor in a Binary Tree. Node lca(Node root, int n1,int n2) { // Your code here if(root==null){ return null; } if(root.data==n1 || root.data==n2){ return root; } Node l=lca(root.left,n1,n2); Node r=lca(root.right,n1,n2); if(l!=null && r!=null){ return root; } if(l!=null){ return l; } if(r!=null){ return r; } return null; } } |