Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

root to leaf sum leetcode


/*
	This is an implementation that demonstrates
	how to check whether a root-to-leaf sum 
	in a binary tree matches some target sum value.

	Let n be the number of binary tree nodes

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

public class RootToLeafSum {
	private BTNode BTRoot;

	public RootToLeafSum() {
		/*
		 * 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;
	}

	public static void main(String[] args) {
		RootToLeafSum application = new RootToLeafSum();
		System.out.println(
				application.hasRootToLeafSum(9)); // true
		System.out.println(
				application.hasRootToLeafSum(7)); // false
	}

	private boolean hasRootToLeafSum(int sum) {
		return hasPathSum(BTRoot, sum);
	}

	private boolean hasPathSum(BTNode root, int sum) {
		// Traverse the tree while constantly update sum
		if (root == null) {
			return false;
		}
		// Deduct current node's value from sum
		sum -= root.val;
		// Sum should only reach zero if a leaf node is reached
		if (root.left == null && root.right == null) {
			return sum == 0;
		}
		// Otherwise continue traversing the tree
		return hasPathSum(root.left, sum) ||
				hasPathSum(root.right, sum);
	}

	// 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
Java :: reentrantlock java 
Java :: print values of bst java 
Java :: How to print in console java 
Java :: all possible valid IP addresses leetcode 
Java :: running sum of 1d array java 
Java :: sum of a list using for loop in dart 
Java :: 8.1.1. Declaring an Array¶ 
Java :: Delete end element in string java 
Java :: spring.jpa.properties.hibernate 
Java :: How to efficiently find the next greater permutation of a list of values, in Java? 
Java :: No Java executable found in current PATH: /bin:/usr/bin:/sbin:/usr/sbin 
Java :: java break 2 for loops 
Java :: java producer consumer 
Java :: How to efficiently shift a linked list by k positions, in Java? 
Java :: java replace first occurrence of substring 
Java :: min height bst leetcode 
Java :: arrays.aslist 
Java :: how to remove null values in java 
Java :: java append file 
Java :: how to right align in java 
Java :: how to find the size of a queue in java 
Java :: java check palindrome with string builder 
Java :: loop through treeset java 
Java :: parameterized constructor 
Java :: how to operate on values from different classes in java 
Java :: Converting String Array to an Integer Array 
Java :: java convert double to int 
Java :: java letter alphabet index 
Java :: java code for square 
Java :: java combobox get selected item 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =