Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Lowest Common Ancestor leetcode


/*
	This is an implementation that demonstrates
	how to efficiently find the lowest common 
	ancestor of two given nodes	(denoted by 
	targetNode1 and targetNode2) in a binary tree. 
	
	Let n be the number of nodes in the tree.

	Time complexity: O(n) 
	Space complexity: O(n)
*/

public class LowestCommonAncestor {
	private BTNode BTRoot;
	private BTNode targetNode1, targetNode2;

	public LowestCommonAncestor() {
		/*
		 * Create tree below:
		 * * 1
		 * / 
		 * 2 3
		 * * / 
		 * * 4 5
		 */
		BTRoot = new BTNode(1, null, null);
		BTNode rootLeft = new BTNode(2, null, null);
		BTRoot.left = rootLeft;
		BTNode rootRight = new BTNode(3, null, null);
		BTRoot.right = rootRight;
		BTNode rootRightLeft = new BTNode(4, null, null);
		BTNode rootRightRight = new BTNode(5, null, null);
		rootRight.left = rootRightLeft;
		rootRight.right = rootRightRight;
		targetNode1 = rootRightLeft;
		targetNode2 = rootLeft;
	}

	public static void main(String[] args) {
		LowestCommonAncestor application = new LowestCommonAncestor();
		// The below prints 1 as the root is lowest
		// common ancestor for the considered target nodes
		System.out.println(application.findLCA().val); // 1
	}

	private BTNode findLCA() {
		return findLCAUtil(BTRoot, targetNode1, targetNode2);
	}

	private BTNode findLCAUtil(BTNode root, BTNode p, BTNode q) {
		if (root == null) {
			return null;
		}
		// If root is one of target nodes then lca is root
		if (root == p || root == q) {
			return root;
		}

		BTNode left = findLCAUtil(root.left, p, q);
		BTNode right = findLCAUtil(root.right, p, q);
		// If target nodes belong to two different subtrees
		// then root is lca
		if (left != null && right != null) {
			return root;
		}

		if (left == null && right == null) {
			return null;
		}
		// If part of same subtree then non-null
		// output is the lca
		return left == null ? right : left;
	}

	// Class representing a binary tree node
	// with pointers to value, left, and right nodes
	private class BTNode {
		int val;
		BTNode left;
		BTNode right;

		public BTNode(int val, BTNode left, BTNode right) {
			this.val = val;
			this.left = left;
			this.right = right;
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: react write into json file 
Javascript :: node express tutorial 
Javascript :: exponent javascript 
Javascript :: mongoose search query for a word in a field 
Javascript :: rating calculator formula javascript 
Javascript :: check if browser is chrome mobile 
Javascript :: convert matrix string to matrix javascript 
Javascript :: JavaScript Creating Symbol 
Javascript :: promises in es6 
Javascript :: destructuring 
Javascript :: how to hide footer in specefic pages in react router 
Javascript :: d3.js click event 
Javascript :: how to check element has event or not in jquery 
Javascript :: access text inside a button from js 
Javascript :: adding cors parameters to extjs ajax 
Javascript :: universal mobile number regex 
Javascript :: string to object js 
Javascript :: preventing form from submitting 
Javascript :: When JavaScript was invented month?* 
Javascript :: how to get today in moment js 
Javascript :: loading page for all ajax call in jquery 3.3.1 
Javascript :: javascript sort 2d array 
Javascript :: MSSQL JSON key value 
Javascript :: js reverse 
Javascript :: how to edit a fil with vanilla js 
Javascript :: make input bigger if text does not fit 
Javascript :: if isset handlebars js 
Javascript :: javascript string reverse 
Javascript :: alpine js x-on click not working 
Javascript :: update in sequelize 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =