Untitled
unknown
plain_text
a month ago
613 B
3
Indexable
TreeNode findKthAncestor(TreeNode root,int[] k,TreeNode x) { if(root == null) return null; if(root == x) return root; Node left = findKthAncestor(root.left,k,x); // Prune as per dry run if(left!=null) { --k[0]; if(k[0] == 0) return root; else return left; } Node right = findKthAncestor(root.right,k,x); if(right!=null) { --k[0]; if(k[0] == 0) return root; else return right; } return null; }
Editor is loading...
Leave a Comment