二叉树的前中后序遍历-递归
递归 前中后序 遍历二叉树
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
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);
//res.add(root.val); 中序遍历
preorder(root.right, res);
res.add(root.val); // 后序遍历
}
}
迭代法—前中后序二叉树遍历
前序遍历
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root == null){
return res;
}
Deque<TreeNode> stack = new LinkedList<>();
TreeNode node = root;
while(!stack.isEmpty() || node!=null){
while(node!=null){
res.add(node.val);
stack.push(node);
node = node.left;
}
node=stack.pop();
node=node.right;
}
return res;
}
}
中序遍历
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root==null){
return res;
}
Deque<TreeNode> stack = new LinkedList<>();
TreeNode node = root;
while(!stack.isEmpty()||node!=null){
while(node!=null){
stack.push(node);
node=node.left;
}
node=stack.pop();
res.add(node.val);
node=node.right;
}
return res;
}
}
后序遍历
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if(root == null){
return res;
}
Deque<TreeNode> stack = new LinkedList<>();
TreeNode prev = null;
while(stack.isEmpty()|| root!=null){
while(root!=null){
stack.push(root);
root=root.left;
}
root = stack.pop();
if(root.right == null || root.right == prev ){
res.add(root.val);
prev = root;
root == null;
}else{
stack.push(root);
root=root.right;
}
}
return res;
}
}
层序遍历
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null){
return res;
}
Deque<TreeNode> stack = new LinkedList<>();
stack.offer(root);
while(!stack.isEmpty()){
List<Integer> ans = new ArrayList<>();
int cur = stack.size();
for(int i =0;i<cur;i++){
TreeNode node = stack.pop();
ans.add(node.val);
if(node.left!=null){
stack.offer(node.left);
}
if(node.right!=null){
stack.offer(node.right);
}
}
res.add(ans);
}
return res;
}
}
二叉树的右视图
方法一 层序遍历
我们刚做的层序遍历 这是我们只需要将层序遍历的最后一个数字读出来就好了
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
Deque<TreeNode> stack = new LinkedList<>();
if(root!=null) stack.push(root);
List<Integer> res = new ArrayList<>();
while(!stack.isEmpty()){
int cur = stack.size();
for(int i = 0;i<cur;i++){
TreeNode node = stack.pop();
if(i==cur-1) res.add(node.val);//核心公式
if(node.left!=null) stack.addLast(node.left);
if(node.right!=null) stack.addLast(node.right);
}
}
return res;
}
}
方法二 深度优先遍历
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
// Deque<TreeNode> stack = new LinkedList<>();
// if(root!=null) stack.push(root);
// List<Integer> res = new ArrayList<>();
// while(!stack.isEmpty()){
// int cur = stack.size();
// for(int i = 0;i<cur;i++){
// TreeNode node = stack.pop();
// if(i==cur-1) res.add(node.val);
// if(node.left!=null) stack.addLast(node.left);
// if(node.right!=null) stack.addLast(node.right);
// }
// }
// return res;
//深度优先遍历
Map<Integer,Integer> map = new HashMap<>();
int max_depth = -1;
Deque<TreeNode> nodeStack = new LinkedList<>();
Deque<Integer> depthStack = new LinkedList<>();
nodeStack.push(root);
depthStack.push(0);
while(!nodeStack.isEmpty()){
TreeNode node = nodeStack.pop();
int depth = depthStack.pop();
if(node !=null){
max_depth = Math.max(max_depth,depth);
if(!map.containsKey(depth)){
map.put(depth,node.val);
}
nodeStack.push(node.left);
nodeStack.push(node.right);
depthStack.push(depth+1);
depthStack.push(depth+1);
}
}
List<Integer> res = new ArrayList<>();
for(int i = 0;i<=max_depth;i++){
res.add(map.get(i));
}
return res;
}
}
广度优先搜索
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
// Deque<TreeNode> stack = new LinkedList<>();
// if(root!=null) stack.push(root);
// List<Integer> res = new ArrayList<>();
// while(!stack.isEmpty()){
// int cur = stack.size();
// for(int i = 0;i<cur;i++){
// TreeNode node = stack.pop();
// if(i==cur-1) res.add(node.val);
// if(node.left!=null) stack.addLast(node.left);
// if(node.right!=null) stack.addLast(node.right);
// }
// }
// return res;
//深度优先遍历
Map<Integer,Integer> map = new HashMap<>();
int max_depth = -1;
Queue<TreeNode> nodeStack = new LinkedList<>();
Queue<Integer> depthStack = new LinkedList<>();
nodeStack.add(root);
depthStack.add(0);
while(!nodeStack.isEmpty()){
TreeNode node = nodeStack.remove();
int depth = depthStack.remove();
if(node !=null){
max_depth = Math.max(max_depth,depth);
map.put(depth,node.val);
nodeStack.add(node.left);
nodeStack.add(node.right);
depthStack.add(depth+1);
depthStack.add(depth+1);
}
}
List<Integer> res = new ArrayList<>();
for(int i = 0;i<=max_depth;i++){
res.add(map.get(i));
}
return res;
}
}
二叉树的层平均值
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public List<Double> averageOfLevels(TreeNode root) {
Deque<TreeNode> stack = new LinkedList<>();
if(root!=null) stack.push(root);
List<Double> res = new ArrayList<>();
while(!stack.isEmpty()){
int cur = stack.size();
double sum = 0;
for(int i = 0;i<cur;i++){
TreeNode node = stack.pop();
sum+=node.val;//核心公式
if(node.left!=null) stack.addLast(node.left);
if(node.right!=null) stack.addLast(node.right);
}
res.add(sum/cur);
}
return res;
}
}