普通遍历
leetcode94
前序:根 左 右
中序:左 根 右
后序:左 右 根
求节点
public int getTreeSize(TreeNode root){
if(root == null)
return 0;
return 1 + getTreeSize(root.left) + getTreeSize(root.right);
}
求最大深度
class Solution {
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;
}
}
求最小深度
叶子节点的定义是左孩子和右孩子都为 null 时叫做叶子节点
当 root 节点左右孩子都为空时,返回 1
当 root 节点左右孩子有一个为空时,返回不为空的孩子节点的深度
当 root 节点左右孩子都不为空时,返回左右孩子较小深度的节点值
class Solution {
public int minDepth(TreeNode root) {
if(root == null)
return 0;
int left = minDepth(root.left);
int right = minDepth(root.right);
if(left == null || right == null)
return left + right + 1;
else
return Math.min(left,right) + 1;
}
}
递归实现 DFS
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
traversal(root);
return res;
}
public void traversal(TreeNode root){
if(root == null)
return;
//第一次来到该节点
traversal(root.left);
//第二次来到该节点
res.add(root.val);
traversal(root.right);
//第三次来到该节点
}
}
非递归实现(使用栈)
前序遍历
从栈中弹出一个节点,打印;
先右再左入栈(如果存在)
重复
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.add(root);
while(stack.size() > 0) {
root = stack.pop();
res.add(root.val)
if(root.right != null) {
stack.add(root.right);
if(root.left != null)
stack.add(root.left);
}
return res;
}
}
中序遍历
每棵子树左边界全部进栈,依次弹出节点时,打印,对弹出节点的右树重复
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
while(stack.size() > 0 || root != null) {
//不断往左子树方向走,每走一次就将当前节点保存到栈中
//这是模拟递归的调用
if(root != null) {
stack.add(root);
root = root.left;
//当前节点为空,说明左边走到头了,从栈中弹出节点并保存
//然后转向右边节点,继续上面整个过程
} else {
TreeNode tmp = stack.pop();
res.add(tmp.val);
root = tmp.right;
}
}
return res;
}
}
后序遍历
从栈中弹出一个节点,放入新栈;
先左再右入栈(如果存在)
重复 1
新栈输出
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<TreeNode> restack = new Stack<TreeNode>();
stack.add(root);
while(stack.size() > 0) {
root = stack.pop();
restack.add(root.val)
if(root.left != null) {
stack.add(root.right);
if(root.right != null)
stack.add(root.left);
}
while(!restack.isEmpty())
res.add(restack.pop());
return res;
}
}
层次遍历
求宽度
在这里插入代码片
leetcode102
BFS 理解
非递归(使用队列)
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
if(root==null) {
return new ArrayList<List<Integer>>();
}
List<List<Integer>> res = new ArrayList<List<Integer>>();
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
//将根节点放入队列中,然后不断遍历队列
queue.add(root);
while(queue.size() > 0){
//size 为每一层节点个数
int size = queue.size();
//每一层的List
List<Integer> temp = new ArrayList<>();
//将每层的每个节点添加到list,并且将左右子节点添加到队列
for(int i = 0;i < size; i++){
TreeNode tt = queue.remove();
temp.add(tt.val);
if(tt.left != null)
queue.add(tt.left);
if(tt.right != null)
queue.add(tt.right);
}
res.add(temp);
}
return res;
}
}
class Solution {
List<List<Integer>> res = new ArrayList<List<Integer>>();
public List<List<Integer>> levelOrder(TreeNode root) {
if(root==null) {
return new ArrayList<List<Integer>>();
}
dfs(0,root);
return res;
}
//给每层添加一个list
public void dfs(int index, TreeNode root){
if(root == null)
return;
if(index >= res.size())
res.add(new ArrayList<Integer>());
res.get(index).add(root.val);
index++;
dfs(index, root.left);
dfs(index, root.right);
}
}
之字遍历
leetcode103
DFS
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root == null)
return res;
dfs(0,root,res);
return res;
}
public void dfs(int index,TreeNode root,List<List<Integer>> res){
if(root == null)
return;
if(index >= res.size())
res.add(new ArrayList<Integer>());
if(index % 2 == 0)
res.get(index).add(root.val);
else
res.get(index).add(0,root.val);
dfs(index + 1,root.left,res);
dfs(index + 1,root.right,res);
}
}
BFS
public ArrayList<ArrayList<Integer>> zigzagLevelOrder (TreeNode root) {
// write code here
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(root == null)
return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
boolean isLeft = true;
while(queue.size() > 0){
int size = queue.size();
//每一层的List
ArrayList<Integer> temp = new ArrayList<>();
for(int i = 0; i < size; i++){
TreeNode tt = queue.poll();
if(isLeft)
temp.add(tt.val);
else
temp.add(0,tt.val);
if(tt.left != null)
queue.add(tt.left);
if(tt.right != null)
queue.add(tt.right);
}
res.add(temp);
isLeft = !isLeft;
}
return res;
}