先序遍历

This commit is contained in:
whai 2024-01-14 18:34:07 +08:00
parent 3df5579ab3
commit daeea3e734
4 changed files with 300 additions and 0 deletions

View File

@ -0,0 +1,83 @@
package cn.whaifree.leetCode.Tree;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
/**
* 前序遍历
* @version 1.0
* @Author whai文海
* @Date 2024/1/14 15:12
* @注释
*/
public class LeetCode144 {
@Test
public void test() {
TreeNode treeNode = TreeNode.constructTree(new Integer[]{});
TreeNode.printTree(treeNode);
new Solution1().preorderTraversal(treeNode).forEach(
integer -> System.out.println(integer)
);
}
class Solution {
List<Integer> res = new LinkedList<Integer>();
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null) {
return res;
}
return pre(root);
}
public List<Integer> pre(TreeNode root) {
if (root == null) {
return null;
}
res.add(root.val);
pre(root.left);
pre(root.right);
return res;
}
}
class Solution1 {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<Integer>();
if (root == null) {
return res;
}
// 基于指针
Deque<TreeNode> queue = new LinkedList<>();
// 加入根节点
TreeNode index = root;
queue.push(index);
while (!queue.isEmpty()) {
index = queue.pop();
res.add(index.val);
// 先加入右子树因为是栈后出右边
if (index.right != null) {
queue.push(index.right);
}
// 后加入左子树
if (index.left != null) {
queue.push(index.left);
}
}
return res;
}
}
}

View File

@ -40,6 +40,9 @@ public class ListNode {
* @param head 链表的头节点 * @param head 链表的头节点
*/ */
public static void printList(ListNode head) { public static void printList(ListNode head) {
if (head == null) {
System.out.println("null!");
}
ListNode curr = head; ListNode curr = head;
while (curr != null) { while (curr != null) {
System.out.print(curr.val + " "); System.out.print(curr.val + " ");

View File

@ -0,0 +1,169 @@
package cn.whaifree.leetCode.model;
import java.util.*;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/14 15:07
* @注释
*/
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode() {
}
public TreeNode(int val) {
this.val = val;
}
public TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
// // 打印一棵树
// public void printTree() {
// if (this == null) {
// return;
// }
// System.out.print(this.val + " ");
// if (this.left != null) {
// this.left.printTree();
// }
// if (this.right != null) {
// this.right.printTree();
// }
// }
public static int getTreeDepth(TreeNode root) {
if (root == null) {
return 0;
}
return 1 + Math.max(getTreeDepth(root.left), getTreeDepth(root.right));
}
public static void printTree(TreeNode root) {
int maxLevel = getTreeDepth(root);
printNodeInternal(Collections.singletonList(root), 1, maxLevel);
}
private static void printNodeInternal(List<TreeNode> nodes, int level, int maxLevel) {
if (nodes == null || nodes.isEmpty() || isAllElementsNull(nodes)) {
return;
}
int floor = maxLevel - level;
int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0)));
int firstSpaces = (int) Math.pow(2, (floor)) - 1;
int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1;
printWhitespaces(firstSpaces);
List<TreeNode> newNodes = new ArrayList<TreeNode>();
for (TreeNode node : nodes) {
if (node != null) {
System.out.print(node.val);
newNodes.add(node.left);
newNodes.add(node.right);
} else {
newNodes.add(null);
newNodes.add(null);
System.out.print(" ");
}
printWhitespaces(betweenSpaces);
}
System.out.println("");
for (int i = 1; i <= endgeLines; i++) {
for (int j = 0; j < nodes.size(); j++) {
printWhitespaces(firstSpaces - i);
if (nodes.get(j) == null) {
printWhitespaces(endgeLines + endgeLines + i + 1);
continue;
}
if (nodes.get(j).left != null) {
System.out.print("/");
} else {
printWhitespaces(1);
}
printWhitespaces(i + i - 1);
if (nodes.get(j).right != null) {
System.out.print("\\");
} else {
printWhitespaces(1);
}
printWhitespaces(endgeLines + endgeLines - i);
}
System.out.println("");
}
printNodeInternal(newNodes, level + 1, maxLevel);
}
private static void printWhitespaces(int count) {
for (int i = 0; i < count; i++) {
System.out.print(" ");
}
}
private static <T> boolean isAllElementsNull(List<T> list) {
for (Object object : list) {
if (object != null) {
return false;
}
}
return true;
}
public static TreeNode constructTree(Integer[] array) {
if (array == null || array.length == 0 || array[0] == null) {
return null;
}
int index = 0;
int length = array.length;
TreeNode root = new TreeNode(array[0]);
Deque<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.offer(root);
TreeNode currNode;
while (index < length) {
index++;
if (index >= length) {
return root;
}
currNode = nodeQueue.poll();
Integer leftChild = array[index];
if (leftChild != null) {
currNode.left = new TreeNode(leftChild);
nodeQueue.offer(currNode.left);
}
index++;
if (index >= length) {
return root;
}
Integer rightChild = array[index];
if (rightChild != null) {
currNode.right = new TreeNode(rightChild);
nodeQueue.offer(currNode.right);
}
}
return root;
}
}

View File

@ -1,5 +1,8 @@
package cn.whaifree.redo.redo_24_1_13; package cn.whaifree.redo.redo_24_1_13;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
/** /**
* @version 1.0 * @version 1.0
* @Author whai文海 * @Author whai文海
@ -7,4 +10,46 @@ package cn.whaifree.redo.redo_24_1_13;
* @注释 * @注释
*/ */
public class LeetCode203 { public class LeetCode203 {
@Test
public void test() {
ListNode listNode = new Solution().removeElements(ListNode.listNodeFromArray(new int[]{7,7,7,7}), 7);
ListNode.printList(listNode);
}
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head == null) {
return null;
}
ListNode pre = new ListNode(0, head);
ListNode remove = remove(pre, val);
return remove.next;
}
public ListNode remove(ListNode head, int val) {
if (head == null) {
return null;
}
// ListNode listNode = removeElements(head.next, val);
/**
* 只考虑最后两个节点
* A B null
*
*/
head.next = remove(head.next, val);
if (head.val == val) {
return head.next;
} else {
return head;
}
}
}
} }