leetcode144 二叉树的前序遍历
递归版本
java
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
preorder(root, res);
return res;
}
public void preorder(TreeNode root, List<Integer> res) {
if (root == null) {
return;
}
res.add(root.val);
preorder(root.left, res);
preorder(root.right, res);
}
非递归版本:利用栈模拟
java
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
//利用栈模拟非递归遍历
Stack<TreeNode> stack = new Stack<>();
if (root == null) {
return res;
}
stack.push(root);
while (!stack.isEmpty()) {
TreeNode pop = stack.pop();
res.add(pop.val);
if (pop.right != null) {
stack.push(pop.right);
}
if (pop.left != null) {
stack.push(pop.left);
}
}
return res;
}
leetcode94 二叉树的中序遍历
java
List<Integer> list = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
mid(root);
return list;
}
private void mid(TreeNode root) {
if (root == null) return;
mid(root.left);
list.add(root.val);
mid(root.right);
}
非递归版本
java
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
result.add(cur.val);
cur = cur.right;
}
}
return result;
}
leetcode145 二叉树的后序遍历
java
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList();
if (root == null) {
return list;
}
postorder(root, list);
return list;
}
private void postorder(TreeNode root, List<Integer> list) {
if (root == null) {
return;
}
postorder(root.left, list);
postorder(root.right, list);
list.add(root.val);
}
非递归版本
java
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
res.add(node.val);
if (node.left != null) {
stack.push(node.left);
}
if (node.right != null) {
stack.push(node.right);
}
}
Collections.reverse(res);
return res;
}
leetcode102 二叉树的层序遍历
java
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root == null) {
return res;
}
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> temp = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
temp.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
res.add(temp);
}
return res;
}
leetcode199 二叉树的右视图
在层序遍历中只加入该层的最后一个节点
java
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root == null) {
return res;
}
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode poll = queue.poll();
if (poll.left != null) {
queue.add(poll.left);
}
if (poll.right != null) {
queue.add(poll.right);
}
if (i == size - 1) {
res.add(poll.val);
}
}
}
return res;
}
leetcode637 二叉树层的平均值
java
public List<Double> averageOfLevels(TreeNode root) {
List<Double> res = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root == null) {
return res;
}
queue.add(root);
while (!queue.isEmpty()) {
double temp = 0;
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
temp += node.val;
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
temp = temp / size;
res.add(temp);
}
return res;
}
leetcode429 N叉树的层序遍历
java
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> res = new ArrayList<>();
Queue<Node> queue = new LinkedList<>();
if (root == null) {
return res;
}
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> temp = new ArrayList<>();
for (int i = 0; i < size; i++) {
Node poll = queue.poll();
temp.add(poll.val);
List<Node> children = poll.children;
for (int j = 0; j < children.size(); j++) {
if (children.get(j) != null) {
queue.add(children.get(j));
}
}
}
res.add(temp);
}
return res;
}
leetcode515 在每个树行中的最大值
java
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
int temp = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNode poll = queue.poll();
temp = Math.max(temp, poll.val);
if (poll.left != null) {
queue.add(poll.left);
}
if (poll.right != null) {
queue.add(poll.right);
}
}
res.add(temp);
}
return res;
}
leetcode116 填充每个节点的右侧指针
java
public Node connect(Node root) {
Queue<Node> queue = new LinkedList<>();
if (root == null) {
return null;
}
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
Node poll = queue.poll();
if (poll.left != null) {
queue.add(poll.left);
}
if (poll.right != null) {
queue.add(poll.right);
}
for (int i = 1; i < size; i++) {
Node cur = queue.poll();
if (cur.left != null) {
queue.add(cur.left);
}
if (cur.right != null) {
queue.add(cur.right);
}
poll.next = cur;
poll = cur;
}
}
return root;
}
leetcode104 二叉树的最大深度
java
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left, right) + 1;
}
leetcode226 翻转二叉树
java
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return root;
}
TreeNode t = root.left;
root.left = root.right;
root.right = t;
invertTree(root.left);
invertTree(root.right);
return root;
}
leetcode101 对称二叉树
java
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return check(root.left, root.right);
}
public boolean check(TreeNode left, TreeNode right) {
if (left == null && right != null) {
return false;
} else if (left != null && right == null) {
return false;
} else if (left == null && right == null) {
return true;
} else if (left.val != right.val) {
return false;
}
//比较外侧节点
boolean bool1 = check(left.left, right.right);
//比较内侧节点
boolean bool2 = check(left.right, right.left);
return bool1 && bool2;
}