Skip to content

Commit 1eedead

Browse files
author
Prashant Jain
committed
Added tree, binary tree, dfs
1 parent 69dcc15 commit 1eedead

7 files changed

Lines changed: 168 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package in.knowledgegate.dsa.binarysearchtree;
2+
3+
import in.knowledgegate.dsa.binarytree.model.TreeNode;
4+
5+
/**
6+
* You are given the root of a binary search tree
7+
* (BST) and an integer val.
8+
*
9+
* Find the node in the BST that the node's value
10+
* equals val and return the subtree rooted with
11+
* that node. If such a node does not exist,
12+
* return null.
13+
*
14+
* Example 1:
15+
* 4
16+
* 2 7
17+
* 1 3 X X
18+
* Input: root = [4,2,7,1,3], val = 2
19+
* Output: [2,1,3]
20+
*
21+
* Example 2:
22+
* 4
23+
* 2 7
24+
* 1 3 X X
25+
* Input: root = [4,2,7,1,3], val = 5
26+
* Output: []
27+
*
28+
*
29+
* Constraints:
30+
*
31+
* The number of nodes in the tree is in the
32+
* range [1, 5000].
33+
* 1 <= Node.val <= 107
34+
* root is a binary search tree.
35+
* 1 <= val <= 107
36+
*/
37+
public class SearchInBST {
38+
public TreeNode searchBST(TreeNode root, int val) {
39+
if (root == null) return null;
40+
if (root.val == val) return root;
41+
if (root.val < val) {
42+
return searchBST(root.right, val);
43+
}
44+
return searchBST(root.left, val);
45+
}
46+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package in.knowledgegate.dsa.binarytree;
2+
3+
import in.knowledgegate.dsa.binarytree.model.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Stack;
8+
9+
/**
10+
* Write Inorder traversal of a binary tree using
11+
* recursive approach
12+
*/
13+
public class InorderTraversalIterative {
14+
public List<Integer> inorderTraversal(TreeNode root) {
15+
Stack<TreeNode> stack = new Stack<>();
16+
List<Integer> result = new ArrayList<>();
17+
TreeNode curr = root;
18+
19+
while (curr != null || !stack.isEmpty()) {
20+
while (curr != null) {
21+
stack.push(curr);
22+
curr = curr.left;
23+
}
24+
curr = stack.pop();
25+
result.add(curr.val);
26+
curr = curr.right;
27+
}
28+
return result;
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package in.knowledgegate.dsa.binarytree;
2+
3+
import in.knowledgegate.dsa.binarytree.model.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* Write Inorder traversal of a binary tree using
10+
* recursive approach
11+
*/
12+
public class InorderTraversalRecursive {
13+
public List<Integer> inorderTraversal(TreeNode root) {
14+
List<Integer> result = new ArrayList<>();
15+
inorderTraversal(root, result);
16+
return result;
17+
}
18+
private void inorderTraversal(TreeNode root,
19+
List<Integer> result) {
20+
if (root == null) return;
21+
inorderTraversal(root.left, result);
22+
result.add(root.val);
23+
inorderTraversal(root.right, result);
24+
}
25+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package in.knowledgegate.dsa.binarytree.model;
2+
3+
public class TreeNode {
4+
public int val;
5+
public TreeNode left;
6+
public TreeNode right;
7+
TreeNode() {}
8+
TreeNode(int val) { this.val = val; }
9+
TreeNode(int val, TreeNode left, TreeNode right) {
10+
this.val = val;
11+
this.left = left;
12+
this.right = right;
13+
}
14+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package in.knowledgegate.dsa.dfs;
2+
3+
import in.knowledgegate.dsa.binarytree.model.TreeNode;
4+
5+
/**
6+
* Given the root of a binary tree and an integer
7+
* targetSum, return true if the tree has a
8+
* root-to-leaf path such that adding up all
9+
* the values along the path equals targetSum.
10+
*
11+
* A leaf is a node with no children.
12+
*
13+
* Example 1:
14+
* 1
15+
* 2 3
16+
*
17+
* Input: root = [1,2,3], targetSum = 5
18+
* Output: false
19+
* Explanation: There two root-to-leaf paths in the tree:
20+
* (1 --> 2): The sum is 3.
21+
* (1 --> 3): The sum is 4.
22+
* There is no root-to-leaf path with sum = 5.
23+
*
24+
* Input: root = [1,2,3], targetSum = 3
25+
* Output: true
26+
*
27+
* Example 2:
28+
* Input: root = [], targetSum = 0
29+
* Output: false
30+
* Explanation: Since the tree is empty, there
31+
* are no root-to-leaf paths.
32+
*
33+
*
34+
* Constraints:
35+
* The number of nodes in the tree is in the
36+
* range [0, 5000].
37+
* -1000 <= Node.val <= 1000
38+
* -1000 <= targetSum <= 1000
39+
*/
40+
public class PathSum {
41+
public boolean hasPathSum(TreeNode root, int targetSum) {
42+
if (root == null) return false;
43+
if (root.val == targetSum && isLeaf(root)) return true;
44+
int newTargetSum = targetSum - root.val;
45+
return hasPathSum(root.left, newTargetSum) ||
46+
hasPathSum(root.right, newTargetSum);
47+
}
48+
49+
private boolean isLeaf(TreeNode node) {
50+
return node != null && node.left == null
51+
&& node.right == null;
52+
}
53+
}
121 KB
Loading
121 KB
Loading

0 commit comments

Comments
 (0)