Skip to content

Commit 693f794

Browse files
authored
symmetric tree and complete-binary-tree-inserter java (#24)
1 parent 212c665 commit 693f794

File tree

3 files changed

+108
-8
lines changed

3 files changed

+108
-8
lines changed
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class CBTInserter {
11+
TreeNode root;
12+
Queue<TreeNode> q = new LinkedList<TreeNode>();
13+
// This queue is a helper to reduce load on insert function..
14+
public CBTInserter(TreeNode root) {
15+
this.root = root;
16+
Queue<TreeNode> temp = new LinkedList(q);
17+
temp.add(root);
18+
q.add(root);
19+
// Prepare a queue for the for our insert function...
20+
while(!temp.isEmpty()){
21+
TreeNode t = temp.poll();
22+
if(t.left!=null){
23+
temp.add(t.left);
24+
q.add(t.left);
25+
}
26+
if(t.right!=null){
27+
temp.add(t.right);
28+
q.add(t.right);
29+
}
30+
}
31+
}
32+
33+
34+
public int insert(int v) {
35+
TreeNode n = new TreeNode(v);
36+
n.left = n.right = null;
37+
TreeNode parent = null;
38+
// Add new node to the queue...
39+
q.add(n);
40+
while(true){
41+
parent = q.peek();
42+
// If the node's left is null then, insert there.
43+
if(parent.left==null){
44+
parent.left = n;
45+
break;
46+
}
47+
// If the node's right is null then, insert there....
48+
// Now this node can never become parent of new node, so remove it from the queue
49+
if(parent.right==null){
50+
parent.right = n;
51+
q.poll();
52+
break;
53+
}
54+
// else it means the node has left and right children which are not null... so remove it from queue.
55+
q.poll();
56+
}
57+
// Return the parent's value to the caller function...
58+
return parent.val;
59+
}
60+
61+
public TreeNode get_root() {
62+
return root;
63+
}
64+
}

Java/symmetric-tree.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
* Runtime: 0 ms
10+
* Memory Usage: 37.9 MB
11+
*/
12+
class Solution {
13+
public boolean isSymmetric(TreeNode root) {
14+
/* if root node of tree itself is null, then it's symmetric */
15+
if(root==null)
16+
return true;
17+
return checkMirror(root.left, root.right);
18+
}
19+
public boolean checkMirror(TreeNode lt, TreeNode rt){
20+
/* if root's left and root's right both are null, is then that subtree is symmetric */
21+
if(lt==null && rt==null)
22+
return true;
23+
24+
/* if root's left is null and root's right is not null and vice-versa, is then that subtree is not symmetric */
25+
if((lt!=null && rt==null) || (lt==null && rt!=null))
26+
return false;
27+
28+
/* check if left node's value matches with right node's value, and recursively check for their left and right subtrees */
29+
if(lt.val==rt.val && checkMirror(lt.left,rt.right) && checkMirror(lt.right,rt.left))
30+
return true;
31+
32+
return false;
33+
}
34+
}

README.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,16 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
130130

131131
## Tree
132132

133-
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
134-
| --- | ------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------- | --------- | ---------- | ------------------------------- | ---- |
135-
| 094 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Java](./Java/binary-tree-inorder-traversal.java) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Stack, HashTable | |
136-
| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [Java](./Java/binary-tree-preorder-traversal.java) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Stack | |
137-
| 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Java](./Java/binary-tree-postorder-traversal.java) | _O(n)_ | _O(logn)_ | Hard | Binary Tree, Stack | |
138-
| 103 | [ZigZag Level Order](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) | [JavaScript](./JavaScript/Binary-Tree-ZigZag-Traversal.js) | _O(n)_ | _O(n)_ | Medium | Binary Tree | |
139-
| 129 | [Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) | [Java](./Java/sum-root-to-leaf-numbers.java) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Depth First Search | |
140-
| 307 | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [Java](./Java/Range-Sum-Query-Mutable.java) | _O(logn)_ | _O(n)_ | Medium | Segment Tree | |
133+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
134+
| --- | ------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------- | --------- | ---------- | ---------------------------------------------- | ---- |
135+
| 094 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Java](./Java/binary-tree-inorder-traversal.java) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Stack, HashTable | |
136+
| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [Java](./Java/symmetric-tree.java) | _O(n)_ | _O(n)_ | Easy | Tree, Breadth-first Search, Depth-first Search | |
137+
| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [Java](./Java/binary-tree-preorder-traversal.java) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Stack | |
138+
| 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Java](./Java/binary-tree-postorder-traversal.java) | _O(n)_ | _O(logn)_ | Hard | Binary Tree, Stack | |
139+
| 103 | [ZigZag Level Order](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) | [JavaScript](./JavaScript/Binary-Tree-ZigZag-Traversal.js) | _O(n)_ | _O(n)_ | Medium | Binary Tree | |
140+
| 129 | [Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) | [Java](./Java/sum-root-to-leaf-numbers.java) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Depth First Search | |
141+
| 307 | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [Java](./Java/Range-Sum-Query-Mutable.java) | _O(logn)_ | _O(n)_ | Medium | Segment Tree | |
142+
| 919 | [Complete Binary Tree Inserter](https://leetcode.com/problems/complete-binary-tree-inserter/) | [Java](./Java/complete-binary-tree-inserter.java) | _O(n)_ | _O(n)_ | Medium | Tree | |
141143

142144
<br/>
143145
<div align="right">

0 commit comments

Comments
 (0)