144. 二叉树的前序遍历
借助栈
class Solution {
public List<Integer> preorderTraversal(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.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
}
return res;
}
}
递归
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
preorderhelper(root, res);
return res;
}
public void preorderhelper(TreeNode node, List<Integer> res) {
if (node == null)
return;
res.add(node.val);
preorderhelper(node.left, res);
preorderhelper(node.right, res);
}
}
94. 二叉树的中序遍历
借助栈
栈先进后出,一直左走直到左子树为空,打印栈内节点,右子树重复
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty() || root != null) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
//这里可以new一个新节点,也可以使用root
root = stack.pop();
res.add(root.val);
root = root.right;
}
}
return res;
}
}
递归
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
inorderhelper(root, res);
return res;
}
public void inorderhelper(TreeNode node, List<Integer> res) {
if (node == null)
return;
inorderhelper(node.left, res);
res.add(node.val);
inorderhelper(node.right, res);
}
}
145. 二叉树的后序遍历
非递归
class Solution {
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;
}
}
递归
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
postorderhelper (root, res);
return res;
}
public void postorderhelper(TreeNode root, List<Integer> res) {
if (root == null) return;
postorder(root.left, res);
postorder(root.right, res);
res.add(root.val);
}
}
102. 二叉树的层序遍历
100. 相同的树
简单的递归
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null) return false;
if (p.val != q.val) return false;
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
101. 对称二叉树
//递归
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return dfs(root.left, root.right);
}
public boolean dfs(TreeNode left, TreeNode right) {
if (left == null && right == null) return true;
if (left == null || right == null) return false;
if (left.val != right.val) return false;
return dfs(left.left, right.right) && dfs(left.right, right.left);
}
}
用队列实现,先拿出来再比较
class Solution {
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null || (root.left == null && root.right == null))
return true;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(root.left);
queue.add(root.right);
while (!queue.isEmpty()) {
TreeNode left = queue.removeFirst();
TreeNode right = queue.removeFirst();
if (left == null && right == null)
continue;
if (left == null || right == null)
return false;
if (left.val != right.val)
return false;
queue.add(left.left);
queue.add(right.right);
queue.add(left.right);
queue.add(right.left);
}
return true;
}
}
226. 翻转二叉树
递归
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) return root;
TreeNode node = root.left;
root.left = root.right;
root.right = node;
invertTree(root.left);
invertTree(root.right);
return root;
}
}
借助队列
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) return null;
Deque<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
while(!queue.isEmpty()) {
//tmp暂时存放从队列中获取的第一个树节点
TreeNode tmp = queue.poll();
//节点left暂时存放当前根节点的左子树
//进行交换
TreeNode left = tmp.left;
tmp.left = tmp.right;
tmp.right = left;
if (tmp.left != null) queue.add(tmp.left);
if (tmp.right != null) queue.add(tmp.right);
}
return root;
}
}