二叉树:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
一、二叉树前序遍历:
1.递归(子问题思想):二叉树的前序等于根加上左树的前序加上右树的前序
public list<Integer> preorderTraversal(TreeNode root){
List<Integer> list = new ArrayList<>();
if(root == null)return list;
list.add(root.val);
List<Integer> leftlist = preorderTraversal(root.left);
list.addAll(leftlist);
List<Integer> rightlist = preorderTraversal(root.right);
list.addAll(rightlist);
return list;
}
2.递归(遍历思想):找到一个结点就加到list里
List<Integer> list = new ArrayList<>();
public list<Integer> preorderTraversal(TreeNode root)
help(root);
return list;
}
public void help(TreeNode root){
if(root == null)return;
list.add(root.val);
help(root.left);
help(root.right);
}
3.迭代:
public list<Integer> preorder(TreeNode root){
List<Integer> list = new ArrayList<>();
if(root == null)return list;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
while(cur != null){
list.add(top.val);
stack.push(cur);
cur = cur.left;
}
TreeNode top = stack.pop()
if(top.right != null){
cur = top.right;
}
}
}
二、二叉树中序遍历
1.递归(子问题思想):
public List<Integer> inorderTraversal(TreeNode root)
List<Integer> list = new ArrayList<>();
if (root == null)return list;
List<Integer> listLeft = inorderTraversal(root.left);
list.addAll(listLeft);
list.add(root.val);
List<Integer> listRight = inorderTraversal(root.right);
list.addAll(listRight);
return list;
}
2.递归(遍历思想):
List<Integer> list = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
help(root);
return list;
}
public void help(TreeNode root){
if(root == null)return;
help(root.left);
list.add(root.val);
help(root.right);
}
3.迭代:
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()){
while(cur != null){
stack.push(cur);
cur = cur.left;
}
TreeNode top = stack.pop();
list.add(top.val);
if(top.right != null){
cur = top.right;
}
}
return list;
}
三、二叉树后序遍历
1.递归(子问题思想):
public List<Integer> postorderTraversal(TreeNode root){
List<Integer> list = new ArrayList<>();
if (root == null)return list;
List<Integer> listLeft = postorderTraversal(root.left);
list.addAll(listLeft);
List<Integer> listRight = postorderTraversal(root.right);
list.addAll(listRight);
list.add(root.val);
return list;
}
2.递归(遍历思想):
List<Integer> list = new ArrayList<>();
public List<Integer> postorderTraversal(I.TreeNode root) {
help(root);
return list;
}
public List<Integer> help(I.TreeNode root){
if (root == null)return null;
help(root.left);
help(root.right);
list.add(root.val);
return list;
}
3.迭代:
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
TreeNode prev = null;
while(cur != null || !stack.isEmpty()){
while(cur != null){
stack.push(cur);
cur = cur.left;
}
TreeNode top = stack.peek();
if(top.right != null && top.right != prev){
cur = top.right;
}else{
list.add(stack.pop().val);
prev = top;
}
}
return list;
}
四、层序遍历
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<>();
if (root == null)return ret;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
List<Integer> rowlist = new ArrayList<>();
while(size != 0){
TreeNode cur = queue.poll();
rowlist.add(cur.val);
if(cur.left != null){
queue.add(cur.left);
}
if(cur.right != null){
queue.add(cur.right);
}
size--;
}
ret.add(rowlist);
}
return ret;
}