各种层次遍历衍生

This commit is contained in:
whai 2024-01-19 22:16:35 +08:00
parent 99db282c5d
commit 3247b3f654
2 changed files with 175 additions and 4 deletions

View File

@ -51,14 +51,11 @@ public class LeetCode116 {
node.left.right = new Node(5);
node.right.left = new Node(6);
node.right.right = new Node(7);
System.out.println(new Solution().connect(node));
System.out.println(new Solution1().connect(node));
}
class Solution {
public Node connect(Node root) {
if (root == null) {
return null;
@ -89,4 +86,30 @@ public class LeetCode116 {
}
}
class Solution1 {
/**
* 递归方法
* @param root
* @return
*/
public Node connect(Node root) {
if (root == null) {
return null;
}
circle(root);
return root;
}
void circle(Node root) {
if (root == null) {
return;
}
if (root.left!=null) root.left.next = root.right == null ? null : root.right;
if (root.right!=null) root.right.next = root.next == null ? null : root.next.left;
circle(root.left);
circle(root.right);
}
}
}

View File

@ -0,0 +1,148 @@
package cn.whaifree.leetCode.Tree;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/1/19 14:54
* @注释
*/
public class LeetCode226 {
@Test
public void test() {
TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5});
treeNode.printTree();
TreeNode treeNode1 = new Solution2().invertTree(treeNode);
treeNode1.printTree();
}
class Solution {
public TreeNode invertTree(TreeNode root) {
reverse(root);
return root;
}
public void reverse(TreeNode root) {
if (root == null) {
return;
}
swap(root);
invertTree(root.left);
invertTree(root.right);
}
public void swap(TreeNode root) {
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
}
}
class Solution1 {
/**
* 层次遍历
* @param root
* @return
*/
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return root;
}
Deque<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode pop = queue.pop();
swap(pop);
if (pop.right != null) {
queue.add(pop.right);
}
if (pop.left != null) {
queue.add(pop.left);
}
}
return root;
}
public void swap(TreeNode root) {
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
}
}
class Solution2 {
/**
* 先序遍历
* @param root
* @return
*/
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return root;
}
Deque<TreeNode> stack = new LinkedList<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode pop = stack.pop();
swap(pop);
if (pop.right != null) {
stack.push(pop.right);
}
if (pop.left != null) {
stack.push(pop.left);
}
}
return root;
}
public void swap(TreeNode root) {
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
}
}
class Solution3 {
/**
* 先序遍历
* @param root
* @return
*/
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return root;
}
Deque<TreeNode> stack = new LinkedList<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode pop = stack.pop();
swap(pop);
if (pop.right != null) {
stack.push(pop.right);
}
if (pop.left != null) {
stack.push(pop.left);
}
}
return root;
}
public void swap(TreeNode root) {
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
}
}
}