Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a3ecac0

Browse files
committed
Tree Problems
1 parent a5d76e7 commit a3ecac0

19 files changed

+5427
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.tree;
2+
3+
import com.tree.model.TreeNode;
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
/**
9+
* printLevelorder(tree)
10+
* 1) Create an empty queue q
11+
* 2) temp_node = root /*start from root
12+
* 3)Loop while temp_node is not NULL
13+
*a)print temp_node->data.
14+
*b)Enqueue temp_node’s children(first left then right children)to q
15+
*c)Dequeue a node from q and assign it’s value to temp_node
16+
*/
17+
public class BFSorLevelOrderTraversalOfBinaryTree {
18+
19+
public static void main(String[] args) {
20+
/*
21+
Binary Tree
22+
23+
1
24+
/ \
25+
2 3
26+
/ \
27+
4 5
28+
*/
29+
TreeNode tree = new TreeNode(1);
30+
tree.left = new TreeNode(2);
31+
tree.right = new TreeNode(3);
32+
tree.left.left = new TreeNode(4);
33+
tree.left.right = new TreeNode(5);
34+
35+
bfsOrlevelOrderTraversal(tree);
36+
37+
}
38+
39+
private static void bfsOrlevelOrderTraversal(TreeNode tree) {
40+
41+
if (tree == null) {
42+
return;
43+
}
44+
Queue<TreeNode> queue = new LinkedList();
45+
queue.offer(tree);
46+
while (!queue.isEmpty()) {
47+
TreeNode node = queue.poll();
48+
System.out.println(node.val);
49+
if (node.left != null) {
50+
queue.offer(node.left);
51+
}
52+
if (node.right != null) {
53+
queue.offer(node.right);
54+
}
55+
}
56+
}
57+
}

src/main/java/com/tree/BinaryTreeZigZagLevelOrderTraversal.html

Lines changed: 2163 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.tree;
2+
3+
import com.tree.model.TreeNode;
4+
5+
import java.util.*;
6+
7+
/**
8+
* Description:
9+
* Given a binary tree, return the zigzag level order traversal of its TreeNodes' values. (ie, from left to right, then
10+
* right to left for the next level and alternate between).
11+
*
12+
* For example:
13+
* Given binary tree [3,9,20,null,null,15,7],
14+
* 3
15+
* / \
16+
* 9 20
17+
* / \
18+
* 15 7
19+
* return its zigzag level order traversal as:
20+
* [
21+
* [3],
22+
* [20,9],
23+
* [15,7]
24+
* ]
25+
*
26+
* Algorithm
27+
* 1. Take 2 Stack, one for current level and one for next level.
28+
*
29+
*
30+
* 2. For filling the next Level stack, we have to see whether it need to be filled Left to Right or Right
31+
* to left as we are reading the Level Order spirally.
32+
* 3. When we are reading current level stack, all the children's of current level stack will go to next
33+
* level stack.
34+
* 4. When current level stack has no value and is Empty at that time point current level stack
35+
* reference to hold next level stack values, that is current level stack will now point to next level
36+
* stack and next level stack should be reinitialize for its next level.
37+
* Also, this is the time when we need to alter our reading style for childrens that is it need to be
38+
* read in left - right fashion.
39+
*
40+
*/
41+
42+
public class BinaryTreeZigZagLevelOrderTraversal {
43+
44+
/**
45+
* Time Complexity:
46+
* Space Complexity:
47+
* Runtime: <a href="https://leetcode.com/submissions/detail/250830618/">1 ms</a>.
48+
*
49+
* @param rootTreeNode
50+
* @return
51+
*/
52+
public static void zigZagTraverse(TreeNode rootTreeNode) {
53+
54+
// Base case
55+
if(rootTreeNode==null){
56+
return;
57+
}
58+
59+
Stack<TreeNode> currentLevelStack = new Stack<TreeNode>();
60+
Stack<TreeNode> nextLevelStack = new Stack<TreeNode>();
61+
62+
boolean isLeftRightReading = true;
63+
currentLevelStack.add(rootTreeNode);
64+
65+
while(!currentLevelStack.isEmpty()){
66+
TreeNode n = currentLevelStack.pop();
67+
System.out.print(n.val+ " ");
68+
69+
if(isLeftRightReading){
70+
71+
if(n.left!=null)
72+
nextLevelStack.push(n.left);
73+
74+
if(n.right!=null)
75+
nextLevelStack.push(n.right);
76+
77+
}else{
78+
if(n.right!=null)
79+
nextLevelStack.push(n.right);
80+
81+
if(n.left!=null)
82+
nextLevelStack.push(n.left);
83+
84+
}
85+
86+
if(currentLevelStack.isEmpty()){
87+
isLeftRightReading = !isLeftRightReading;
88+
currentLevelStack=nextLevelStack;
89+
nextLevelStack = new Stack<TreeNode>();
90+
}
91+
92+
}
93+
}
94+
95+
96+
public static void main(String[] args) {
97+
/*
98+
Binary Tree
99+
100+
1
101+
/ \
102+
2 3
103+
/ \
104+
4 5
105+
*/
106+
TreeNode tree = new TreeNode(1);
107+
tree.left = new TreeNode(2);
108+
tree.right = new TreeNode(3);
109+
tree.left.left = new TreeNode(4);
110+
tree.left.right = new TreeNode(5);
111+
112+
zigZagTraverse(tree);
113+
114+
}
115+
}
116+
117+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.tree;
2+
3+
import com.tree.model.TreeNode;
4+
5+
public class CheckBinaryTreeIsBST {
6+
7+
public static void main(String args[]) {
8+
9+
/*
10+
Binary Tree
11+
12+
1
13+
/ \
14+
2 3
15+
/ \
16+
4 5
17+
*/
18+
TreeNode tree = new TreeNode(10);
19+
tree.left = new TreeNode(8);
20+
tree.right = new TreeNode(30);
21+
tree.left.left = new TreeNode(6);
22+
tree.left.right = new TreeNode(9);
23+
tree.right.left = new TreeNode(15);
24+
tree.right.right = new TreeNode(35);
25+
tree.left.left.left = new TreeNode(4);
26+
27+
28+
int min = Integer.MIN_VALUE;
29+
int max = Integer.MAX_VALUE;
30+
31+
System.out.println("Is Binary Search Tree:" + isBST(tree,min,max));
32+
}
33+
34+
/**Returns true if the given tree is a BST and its
35+
values are >= min and <= max. */
36+
private static boolean isBST(TreeNode node, int min, int max) {
37+
38+
/* an empty tree is BST */
39+
if (node == null)
40+
return true;
41+
42+
/**important*/
43+
/** false if this node violates the min/max constraints */
44+
if(node.val < min || node.val >max){
45+
return false;
46+
}
47+
48+
/** otherwise check the subtrees recursively
49+
tightening the min/max constraints */
50+
// Allow only distinct values
51+
return isBST(node.left,min,node.val-1) && isBST(node.right, node.val+1, max);
52+
}
53+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.tree;
2+
3+
4+
import com.tree.model.TreeNode;
5+
6+
import java.util.Stack;
7+
8+
/**
9+
* LEFT *ROOT* RIGHT
10+
*
11+
* It's worth remembering that the inOrder traversal is a depth-first algorithm and
12+
* prints the tree node in sorted order if given binary tree is a binary search tree.
13+
*
14+
* Visit left node
15+
* Print value of the root
16+
* Visit right node
17+
*
18+
*/
19+
public class DFSInorderTraversalOfTree {
20+
21+
public static void main (String args[]){
22+
TreeNode root= create();
23+
inOrderTraversal(root);
24+
inorderStack(root);
25+
}
26+
27+
/**
28+
* Recurssive Approach
29+
* @param root
30+
*/
31+
private static void inOrderTraversal(TreeNode root) {
32+
if (root == null){
33+
return;
34+
}
35+
inOrderTraversal(root.left);
36+
System.out.print(root.val + " ");
37+
inOrderTraversal(root.right);
38+
}
39+
40+
/**Iterative Approach*/
41+
public static void inorderStack(TreeNode root)
42+
{
43+
Stack<TreeNode> s =new Stack<TreeNode>();
44+
while(true){
45+
46+
// Go to the left extreme insert all the elements to stack
47+
while(root != null){
48+
s.push(root);
49+
root = root.left;
50+
}
51+
// check if Stack is empty, if yes, exit from everywhere
52+
if (s.isEmpty()) {
53+
return;
54+
}
55+
// pop the element from the stack , print it and add the nodes at
56+
// the right to the Stack
57+
root =s.pop();
58+
System.out.print(root.val + " ");
59+
root = root.right;
60+
}
61+
}
62+
63+
64+
/**
65+
* Java method to create binary tree with test data
66+
*
67+
* @return a sample binary tree for testing
68+
*/
69+
public static TreeNode create() {
70+
TreeNode root = new TreeNode(40);
71+
root.left = new TreeNode(20);
72+
root.left.left = new TreeNode(10);
73+
root.left.left.left = new TreeNode(5);
74+
root.left.right = new TreeNode(30);
75+
root.right = new TreeNode(50);
76+
root.right.right = new TreeNode(60);
77+
root.left.right.left = new TreeNode(67);
78+
root.left.right.right = new TreeNode(78);
79+
return root;
80+
}
81+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.tree;
2+
3+
import com.tree.model.TreeNode;
4+
5+
import java.util.Stack;
6+
7+
/**
8+
* *ROOT* LEFT RIGHT
9+
*/
10+
public class DFSPreOrderTraversalOfBinaryTree {
11+
12+
/**
13+
* Recurssive Approach
14+
*
15+
* @param root
16+
*/
17+
private static void preOrderTraversal(TreeNode root) {
18+
if (root == null) {
19+
return;
20+
}
21+
System.out.print(root.val + " ");
22+
preOrderTraversal(root.left);
23+
preOrderTraversal(root.right);
24+
}
25+
26+
/**
27+
* Iterative Approach
28+
*/
29+
public static void preOrderStack(TreeNode root) {
30+
Stack<TreeNode> s = new Stack<TreeNode>();
31+
32+
while (true) {
33+
// First print the root node and then add left node
34+
while (root != null) {
35+
System.out.print(root.val + " ");
36+
s.push(root);
37+
root = root.left;
38+
}
39+
// check if Stack is emtpy, if yes, exit from everywhere
40+
if (s.isEmpty()) {
41+
return;
42+
}
43+
// pop the element from the stack and go right to the tree
44+
root = s.pop();
45+
root = root.right;
46+
}
47+
}
48+
49+
50+
/**
51+
* Java method to create binary tree with test data
52+
*
53+
* @return a sample binary tree for testing
54+
*/
55+
public static TreeNode create() {
56+
TreeNode root = new TreeNode(40);
57+
root.left = new TreeNode(20);
58+
root.left.left = new TreeNode(10);
59+
root.left.left.left = new TreeNode(5);
60+
root.left.right = new TreeNode(30);
61+
root.right = new TreeNode(50);
62+
root.right.right = new TreeNode(60);
63+
root.left.right.left = new TreeNode(67);
64+
root.left.right.right = new TreeNode(78);
65+
return root;
66+
}
67+
68+
public static void main(String args[]) {
69+
TreeNode root = create();
70+
preOrderStack(root);
71+
}
72+
}

0 commit comments

Comments
 (0)