Skip to content

Commit

Permalink
二叉树的右视图
Browse files Browse the repository at this point in the history
  • Loading branch information
nibnait committed Mar 20, 2020
1 parent 18349ca commit 3697116
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,65 +32,68 @@ public void testCase() {
Integer[] root = {1, 2, 3, null, 5, null, 4};
System.out.println(rightSideView(ConstructBinaryTree.constructByBFSArray(root)));

Integer[] root1 = {1, 2, 3, null, 5, null, 4};
Integer[] root1 = {};
System.out.println(rightSideView(ConstructBinaryTree.constructByBFSArray(root1)));

Integer[] root2 = {1, 2};
System.out.println(rightSideView(ConstructBinaryTree.constructByBFSArray(root2)));

Integer[] root3 = {1, 2, 3, 4};
System.out.println(rightSideView(ConstructBinaryTree.constructByBFSArray(root3)));

}

}

/**
* 1. 层先法遍历二叉树
* 2. 然后取第1、2、4、8、16...个结点上的val
* // 2. 然后取第1、2、4、8、16...个结点上的val
* 2. 遇到每一层的最后一个元素 记录下来。
*/
public List<Integer> rightSideView(TreeNode root) {
List<Integer> bfsArray = new ArrayList<>();

bfsTravel(root, bfsArray);
// System.out.println(JSON.toJSONString(bfsArray));
// PrintBinaryTree.print(root);

List<Integer> result = new ArrayList<>();
int row = 0;
int index = Double.valueOf(Math.pow(2, row)).intValue();
while (index <= bfsArray.size()) {
int rightIndex = index - 1;
Integer node = bfsArray.get(rightIndex);
if (node != null) {
result.add(node);
} else {
while (bfsArray.get(--rightIndex) != null) {
result.add(node);
}
}

index += Double.valueOf(Math.pow(2, ++row)).intValue();
if (root == null) {
return result;
}

return bfsArray;
}

private void bfsTravel(TreeNode root, List<Integer> bfsArray) {

bfsArray.add(root.val);

// queue是当前层的所有节点
LinkedList<TreeNode> queue = new LinkedList<>();
queue.addLast(root.left);
queue.addLast(root.right);
// 下一层的所有节点
LinkedList<TreeNode> nextRowQueue = new LinkedList<>();

int val = root.val;
queue.add(root);

while (!queue.isEmpty()) {
while (!queue.isEmpty()) {
TreeNode treeNode = queue.poll();

TreeNode treeNode = queue.pollFirst();
bfsArray.add(treeNode.val);
if (treeNode != null && treeNode.val != null) {
val = treeNode.val;
}

if (isNotLeafNode(treeNode)) {
queue.addLast(treeNode.left);
queue.addLast(treeNode.right);
if (treeNode != null && !isLeafNode(treeNode)) {
nextRowQueue.add(treeNode.left);
nextRowQueue.add(treeNode.right);
}
}
result.add(val);
copyToQueue(nextRowQueue, queue);
}
return result;
}

private void copyToQueue(LinkedList<TreeNode> nextRowQueue, LinkedList<TreeNode> queue) {
queue.clear();
while (!nextRowQueue.isEmpty()){
queue.add(nextRowQueue.poll());
}
}

private boolean isNotLeafNode(TreeNode treeNode) {
return treeNode.left != null && treeNode.right != null;
private boolean isLeafNode(TreeNode treeNode) {
return treeNode.left == null && treeNode.right == null;
}

}
9 changes: 6 additions & 3 deletions src/main/java/common/util/ConstructBinaryTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ public static TreeNode constructByBFSArray(Integer[] bfsArray) {
Integer leftValue = bfsArray[count++];
pop.left = new TreeNode(leftValue);
queue.add(pop.left);
Integer rightValue = bfsArray[count++];
pop.right = new TreeNode(rightValue);
queue.add(pop.right);

if (count + 1 <= bfsArray.length) {
Integer rightValue = bfsArray[count++];
pop.right = new TreeNode(rightValue);
queue.add(pop.right);
}
}
return head;
}
Expand Down
12 changes: 10 additions & 2 deletions src/main/test/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Functions;
import com.google.common.collect.Lists;
import common.datastruct.TreeNode;
import junit.framework.TestCase;
import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

Expand All @@ -15,8 +17,14 @@ public class Main extends TestCase {

@Test
public void testCase() {
String str = "aabcccccaa";
System.out.println(compressString(str));
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(null);
queue.add(null);
queue.add(null);
queue.add(null);

System.out.println(queue.isEmpty());

}

public String compressString(String S) {
Expand Down

0 comments on commit 3697116

Please sign in to comment.