-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package infrastructure; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 定义 N叉树的结构 | ||
* | ||
* @author EastPlain | ||
* @date 2021/7/27 22:38 | ||
*/ | ||
public class Node { | ||
|
||
public int val; | ||
public List<Node> children; | ||
|
||
public Node() {} | ||
|
||
public Node(int _val) { | ||
val = _val; | ||
} | ||
|
||
public Node(int _val, List<Node> _children) { | ||
val = _val; | ||
children = _children; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package infrastructure; | ||
|
||
/** | ||
* 定义 二叉树的结构 | ||
* | ||
* @author EastPlain | ||
* @date 2021/7/26 21:52 | ||
*/ | ||
public class TreeNode { | ||
|
||
public int val; | ||
public TreeNode left; | ||
public TreeNode right; | ||
|
||
public TreeNode(int val, TreeNode left, TreeNode right) { | ||
this.val = val; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package method; | ||
|
||
import infrastructure.TreeNode; | ||
|
||
import javax.print.DocFlavor; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Stack; | ||
|
||
/** | ||
* 二叉树的中序遍历 | ||
* # lc0094 | ||
* | ||
* @author EastPlain | ||
* @date 2021/7/26 23:18 | ||
*/ | ||
public class BinaryTreeInorder { | ||
|
||
/** | ||
* 递归法 | ||
*/ | ||
public List<Integer> recur(TreeNode root) { | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* 迭代法 | ||
*/ | ||
public List<Integer> iteration(TreeNode root) { | ||
|
||
Stack<TreeNode> stack = new Stack<>(); | ||
List<Integer> ans = new ArrayList<>(); | ||
|
||
if (root == null) { | ||
return ans; | ||
} | ||
|
||
TreeNode current = root; | ||
|
||
while (! stack.isEmpty() || current != null) { | ||
|
||
while (current != null) { | ||
stack.push(current); | ||
current = current.left; | ||
} | ||
|
||
TreeNode node = stack.pop(); | ||
ans.add(node.val); | ||
|
||
if (node.right != null) { | ||
stack.push(node.right); | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package method; | ||
|
||
import infrastructure.TreeNode; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Queue; | ||
|
||
/** | ||
* 二叉树的层序遍历 | ||
* # lc0102 | ||
* | ||
* @author EastPlain | ||
* @date 2021/7/26 23:37 | ||
*/ | ||
public class BinaryTreeLayer { | ||
|
||
public List<List<Integer>> method(TreeNode root) { | ||
|
||
List<List<Integer>> ans = new ArrayList<>(); | ||
Queue<TreeNode> queue = new LinkedList<>(); | ||
|
||
if (root == null) { | ||
return ans; | ||
} | ||
|
||
queue.add(root); | ||
|
||
while (! queue.isEmpty()) { | ||
|
||
int size = queue.size(); | ||
List<Integer> temp = new ArrayList<>(); | ||
|
||
for (int i = 0; i < size; i++) { | ||
TreeNode current = queue.remove(); | ||
temp.add(current.val); | ||
|
||
if (current.left != null) { | ||
queue.add(current.left); | ||
} | ||
|
||
if (current.right != null) { | ||
queue.add(current.right); | ||
} | ||
} | ||
|
||
ans.add(temp); | ||
} | ||
|
||
return ans; | ||
} | ||
} |