统一的迭代法

层次遍历
This commit is contained in:
whai 2024-01-16 21:09:45 +08:00
parent 7268302aec
commit a3f6650e10
2 changed files with 179 additions and 6 deletions

View 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;
}
}
}

View File

@ -4,10 +4,7 @@ import cn.whaifree.leetCode.model.ListNode;
import cn.whaifree.leetCode.model.TreeNode; import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList; import java.util.*;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
/** /**
* @version 1.0 * @version 1.0
@ -19,10 +16,14 @@ public class LeetCode94 {
@Test @Test
public void 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); 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 { 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;
}
}
} }