Skip to content

Commit 212c665

Browse files
authored
Binary Tree Traversals - inorder, preorder, postorder java (#23)
1 parent 96e2c57 commit 212c665

4 files changed

+119
-5
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
* Runtime: 0 ms
11+
* Memory Usage: 37.8 MB
12+
*
13+
*/
14+
15+
class Solution {
16+
public List<Integer> inorderTraversal(TreeNode root) {
17+
List<Integer> list = new ArrayList<Integer>();
18+
inorder(root, list);
19+
return list;
20+
}
21+
22+
public void inorder(TreeNode root, List<Integer> list){
23+
/* Inorder traversal is visiting : left subtree then root then right subtree */
24+
25+
/* if node is null, then return back to the caller function */
26+
if(root==null)
27+
return;
28+
/* Recurse for left subtree */
29+
inorder(root.left, list);
30+
31+
/* Add the current node's value to the list */
32+
list.add(root.val);
33+
34+
/* Recurse for right subtree */
35+
inorder(root.right, list);
36+
}
37+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
* Runtime: 0 ms
11+
* Memory Usage: 37.9 MB
12+
*
13+
*/
14+
class Solution {
15+
List<Integer> list = new ArrayList<Integer>();
16+
public List<Integer> postorderTraversal(TreeNode root) {
17+
postorder(root);
18+
return list;
19+
}
20+
21+
public void postorder(TreeNode root){
22+
/* Postorder traversal is visiting : left subtree then right subtree then root */
23+
24+
/* if node is null, then return back to the caller function */
25+
if(root==null)
26+
return;
27+
28+
/* Recurse for left subtree */
29+
postorderTraversal(root.left);
30+
31+
/* Recurse for right subtree */
32+
postorderTraversal(root.right);
33+
34+
/* Add the current node's value to the list */
35+
list.add(root.val);
36+
}
37+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
* Runtime: 0 ms
11+
* Memory Usage: 37.9 MB
12+
*
13+
*/
14+
15+
class Solution {
16+
public List<Integer> preorderTraversal(TreeNode root) {
17+
List<Integer> ls = new ArrayList<Integer>();
18+
preorder(root, ls);
19+
return ls;
20+
}
21+
public void preorder(TreeNode root, List<Integer> ls){
22+
/* Preorder traversal is visiting : root then left subtree then right subtree */
23+
24+
/* if node is null, then return back to the caller function */
25+
if(root==null)
26+
return;
27+
28+
/* Add the current node's value to the list */
29+
ls.add(root.val);
30+
31+
/* Recurse for left subtree */
32+
preorder(root.left, ls);
33+
34+
/* Recurse for right subtree */
35+
preorder(root.right, ls);
36+
}
37+
}

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,14 @@ 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-
| 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 | |
136-
| 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 | |
137-
| 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+
| 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 | |
138141

139142
<br/>
140143
<div align="right">

0 commit comments

Comments
 (0)