统一的迭代法
层次遍历
This commit is contained in:
parent
7268302aec
commit
a3f6650e10
50
src/main/java/cn/whaifree/leetCode/Tree/LeetCode102.java
Normal file
50
src/main/java/cn/whaifree/leetCode/Tree/LeetCode102.java
Normal file
@ -0,0 +1,50 @@
|
||||
package cn.whaifree.leetCode.Tree;
|
||||
|
||||
import cn.whaifree.leetCode.model.TreeNode;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
* @Author whai文海
|
||||
* @Date 2024/1/16 20:37
|
||||
* @注释
|
||||
*/
|
||||
public class LeetCode102 {
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
System.out.println(new Solution().levelOrder(TreeNode.constructTree(new Integer[]{3,9,20,null,null,15,7})));
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
|
||||
List<List<Integer>> res = new LinkedList<>();
|
||||
if (root == null) {
|
||||
return res;
|
||||
}
|
||||
Deque<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
while (!queue.isEmpty()) {
|
||||
// 遍历本层的个数
|
||||
List<Integer> e = new ArrayList<>();
|
||||
int size = queue.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
TreeNode pop = queue.pop();
|
||||
e.add(pop.val);
|
||||
if(pop.left!=null) queue.add(pop.left);
|
||||
if(pop.right!=null) queue.add(pop.right);
|
||||
}
|
||||
res.add(e);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
@ -4,10 +4,7 @@ import cn.whaifree.leetCode.model.ListNode;
|
||||
import cn.whaifree.leetCode.model.TreeNode;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @version 1.0
|
||||
@ -19,10 +16,14 @@ public class LeetCode94 {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5});
|
||||
TreeNode root = TreeNode.constructTree(new Integer[]{1, 2, 3, 4});
|
||||
TreeNode.printTree(root);
|
||||
|
||||
System.out.println(new Solution1().inorderTraversal(root));
|
||||
System.out.println(new Solution2().inorderTraversal(root));
|
||||
System.out.println(new Solution2().preorderTraversal(root));
|
||||
System.out.println(new Solution2().postorderTraversal(root));
|
||||
|
||||
|
||||
}
|
||||
|
||||
class Solution {
|
||||
@ -75,4 +76,126 @@ public class LeetCode94 {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Solution2 {
|
||||
|
||||
|
||||
/**
|
||||
* 不能 前后序统一
|
||||
* 因为中间节点是在中间,每次经过他都不能判断是否是第一次进入
|
||||
* - 如果增加一个标记,标记Null入栈,判断到null时就证明他是已经走过的节点
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
public List<Integer> inorderTraversal(TreeNode root) {
|
||||
|
||||
List<Integer> list = new LinkedList<Integer> ();
|
||||
if (root == null) {
|
||||
return list;
|
||||
}
|
||||
Deque<TreeNode> stack = new LinkedList<>();
|
||||
|
||||
stack.push(root);
|
||||
/**
|
||||
* 右边进栈,再左边进栈,中间用空指针标记
|
||||
*/
|
||||
while (!stack.isEmpty()) {
|
||||
TreeNode pop = stack.pop();
|
||||
// 这个if处理中间节点是否处理过
|
||||
if (pop != null) {
|
||||
if(pop.right!=null) stack.push(pop.right);
|
||||
// 此处null 为标记已经处理过的节点,下次遇到这个节点就可以直接输出了
|
||||
stack.push(pop);
|
||||
stack.push(null);
|
||||
if (pop.left!=null) stack.push(pop.left);
|
||||
} else {
|
||||
TreeNode pop1 = stack.pop();
|
||||
list.add(pop1.val);
|
||||
}
|
||||
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 不能 前后序统一
|
||||
* 因为中间节点是在中间,每次经过他都不能判断是否是第一次进入
|
||||
* - 如果增加一个标记,标记Null入栈,判断到null时就证明他是已经走过的节点
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
|
||||
List<Integer> list = new LinkedList<Integer> ();
|
||||
if (root == null) {
|
||||
return list;
|
||||
}
|
||||
Deque<TreeNode> stack = new LinkedList<>();
|
||||
|
||||
stack.push(root);
|
||||
/**
|
||||
* 右边进栈,再左边进栈,中间用空指针标记
|
||||
*/
|
||||
while (!stack.isEmpty()) {
|
||||
TreeNode pop = stack.pop();
|
||||
|
||||
if (pop != null) {
|
||||
// 变更这三个的顺序就能 前中后序
|
||||
if (pop.right != null) {
|
||||
stack.push(pop.right);
|
||||
}
|
||||
if (pop.left != null) {
|
||||
stack.push(pop.left);
|
||||
}
|
||||
stack.push(pop);
|
||||
stack.push(null);
|
||||
} else {
|
||||
TreeNode pop1 = stack.pop();
|
||||
list.add(pop1.val);
|
||||
}
|
||||
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public List<Integer> postorderTraversal(TreeNode root) {
|
||||
|
||||
List<Integer> list = new LinkedList<Integer> ();
|
||||
if (root == null) {
|
||||
return list;
|
||||
}
|
||||
Deque<TreeNode> stack = new LinkedList<>();
|
||||
|
||||
stack.push(root);
|
||||
/**
|
||||
* 右边进栈,再左边进栈,中间用空指针标记
|
||||
*/
|
||||
while (!stack.isEmpty()) {
|
||||
TreeNode pop = stack.pop();
|
||||
|
||||
if (pop != null) {
|
||||
|
||||
stack.push(pop);
|
||||
stack.push(null);
|
||||
if (pop.right != null) {
|
||||
stack.push(pop.right);
|
||||
}
|
||||
if (pop.left != null) {
|
||||
stack.push(pop.left);
|
||||
}
|
||||
|
||||
} else {
|
||||
TreeNode pop1 = stack.pop();
|
||||
list.add(pop1.val);
|
||||
}
|
||||
|
||||
}
|
||||
return list;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user