目录
144 二叉树的前序遍历
递归遍历
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<>();
traversal(root,res);
return res;
}
void traversal(TreeNode root,ArrayList<Integer> res){
if(root == null)return;
res.add(root.val);//将该节点的val加入res中
traversal(root.left,res);
traversal(root.right,res);
}
}
时间复杂度O(n)每个节点都被遍历一次
空间复杂度O(n) 为递归情况下栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)
迭代遍历一
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
Stack<TreeNode>stack = new Stack<>();
ArrayList<Integer>res = new ArrayList<>();
if(root == null)return res;
stack.push(root);
while(!stack.isEmpty()){
TreeNode temp = stack.pop();
res.add(temp.val);
if(temp.right != null)stack.push(temp.right);
if(temp.left != null)stack.push(temp.left);
}
return res;
}
}
时间复杂度O(n)每个节点都被遍历一次
空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)
迭代遍历二
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode>stack = new Stack<>();
if(root != null)stack.push(root);
while(!stack.isEmpty()){
TreeNode cur = stack.pop();
if(cur != null){
if(cur.right != null)stack.push(cur.right);
if(cur.left != null)stack.push(cur.left);
stack.push(cur);
stack.push(null);
}else{
cur = stack.pop();
res.add(cur.val);
}
}
return res;
}
}
时间复杂度O(n)
空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)
145 二叉树的后序遍历
递归遍历
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer>res = new ArrayList<>();
traversal(root,res);
return res;
}
void traversal(TreeNode root,List<Integer> res){
if(root == null)return;
traversal(root.left,res);
traversal(root.right,res);
res.add(root.val);
}
}
时间复杂度O(n)每个节点都被遍历一次
空间复杂度O(n) 为递归情况下栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)
迭代遍历一
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer>res = new ArrayList<>();
Stack<TreeNode>stack = new Stack<>();
if(root == null)return res;
stack.push(root);
while(!stack.isEmpty()){
TreeNode temp = stack.pop();
if(temp.left != null)stack.push(temp.left);
if(temp.right != null)stack.push(temp.right);
res.add(temp.val);
}
Collections.reverse(res);
return res;
}
}
时间复杂度O(n)每个节点都被遍历一次
空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)
迭代遍历二
class Solution {//左右中
public List<Integer> postorderTraversal(TreeNode root) {
Stack<TreeNode>stack = new Stack<>();
ArrayList<Integer>res = new ArrayList<>();
if(root != null)stack.push(root);
while(!stack.isEmpty()){
TreeNode cur = stack.pop();
if(cur != null){
stack.push(cur);
stack.push(null);
if(cur.right != null)stack.push(cur.right);
if(cur.left != null)stack.push(cur.left);
}else{
cur = stack.pop();
res.add(cur.val);
}
}
return res;
}
}
时间复杂度O(n)
空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)
94 二叉树的中序遍历
递归遍历
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer>res = new ArrayList<>();
traversal(root,res);
return res;
}
void traversal(TreeNode root,List<Integer>res){
if(root == null)return;
traversal(root.left,res);
res.add(root.val);
traversal(root.right,res);
}
}
时间复杂度O(n)每个节点都被遍历一次
空间复杂度O(n) 为递归情况下栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)
迭代遍历一
class Solution {//左中右
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer>res = new ArrayList<>();
Stack<TreeNode>stack = new Stack<>();
while(root != null || !stack.isEmpty()){
while(root != null){
stack.push(root);
root = root.left;
}
root = stack.pop();
res.add(root.val);
root = root.right;
}
return res;
}
}
时间复杂度O(n)每个节点都被遍历一次
空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)
迭代遍历二
class Solution {//左中右
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer>res = new ArrayList<>();
Stack<TreeNode>stack = new Stack<>();
if(root != null)stack.push(root);
while(!stack.isEmpty()){
TreeNode cur = stack.pop();
if(cur != null){
if(cur.right != null)stack.push(cur.right);
stack.push(cur);
stack.push(null);
if(cur.left != null)stack.push(cur.left);
}else{
cur = stack.pop();
res.add(cur.val);
}
}
return res;
}
}
时间复杂度O(n)
空间复杂度O(n) 为迭代过程中栈的开销,平均情况下为O(logn),最坏情况下树成链状,为O(n)