二叉树的前序,中序,后序,层序遍历(递归与非递归)
class Solution {
List<Integer> res = new ArrayList();
public List<Integer> preorderTraversal(TreeNode root) {
preorder(root);
return res;
}
public void preorder(TreeNode root) {
if (root != null) {
res.add(root.val);
preorder(root.left);
preorder(root.right);
}
}
}
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList();
Stack<TreeNode> stack = new Stack();
while (!stack.isEmpty() || root != null) {
if (root != null) {
res.add(root.val);
stack.push(root);
root = root.left;
} else {
root = stack.pop();
root = root.right;
}
}
return res;
}
}
class Solution {
List<Integer> res = new ArrayList();
public List<Integer> inorderTraversal(TreeNode root) {
inorder(root);
return res;
}
public void inorder(TreeNode root) {
if (root != null) {
inorder(root.left);
res.add(root.val);
inorder(root.right);
}
}
}
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList();
Stack<TreeNode> stack = new Stack();
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.pop();
res.add(root.val);
root = root.right;
}
}
return res;
}
}
class Solution {
List<Integer> res = new ArrayList();
public List<Integer> postorderTraversal(TreeNode root) {
postorder(root);
return res;
}
public void postorder(TreeNode root) {
if (root != null) {
postorder(root.left);
postorder(root.right);
res.add(root.val);
}
}
}
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList();
Stack<TreeNode> stack = new Stack();
TreeNode prev = null;
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
root = stack.peek();
if (root.right == null || root.right == prev) {
res.add(root.val);
stack.pop();
prev = root;
root = null;
} else {
root = root.right;
}
}
}
return res;
}
}
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList();
if (root == null) return result;
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> level = new ArrayList();
while (size > 0) {
TreeNode temp = queue.poll();
size--;
level.add(temp.val);
if (temp.left != null) queue.offer(temp.left);
if (temp.right != null) queue.offer(temp.right);
}
result.add(level);
}
return result;
}
}
二叉树的右视图
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList();
if (root == null) return result;
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
while (size > 0) {
TreeNode node = queue.poll();
if (size == 1) result.add(node.val);
size--;
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
}
return result;
}
}
二叉树的层平均值
class Solution {
public List<Double> averageOfLevels(TreeNode root) {
List<Double> result = new ArrayList();
if (root == null) return result;
Queue<TreeNode> queue = new LinkedList();
queue.offer(root);
while (!queue.isEmpty()) {
long sum = 0;
int size = queue.size();
int num = size;
while (size > 0) {
TreeNode node = queue.poll();
sum += node.val;
size--;
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
result.add((double) sum / num);
}
return result;
}
}
填充每个节点的下一个右侧节点指针
class Solution {
public Node connect(Node root) {
if (root != null) {
Queue<Node> queue = new LinkedList();
queue.offer(root);
Node pre = null;
Node cur = null;
while (!queue.isEmpty()) {
int size = queue.size();
while (size > 0) {
cur = queue.poll();
size--;
if (pre != null) pre.next = cur;
pre = cur;
if (cur.left != null) queue.offer(cur.left);
if (cur.right != null) queue.offer(cur.right);
}
pre = null;
}
}
return root;
}
}
class Solution {
public Node connect(Node root) {
if (root != null) {
Queue<Node> queue = new LinkedList();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
Node cur = queue.poll();
if (i < size - 1) cur.next = queue.peek();
if (cur.left != null) queue.offer(cur.left);
if (cur.right != null) queue.offer(cur.right);
}
}
}
return root;
}
}