二叉树的递归遍历
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> list = new ArrayList<>();if(root==null) {return list;}else {traversal(list,root);}return list;}public void traversal(List<Integer> list,TreeNode root) {if(root==null) {return;}//前中后序调整一下下面的顺序即可list.add(root.val);traversal(list,root.left);traversal(list,root.right);}
}
二叉树的迭代遍历
思路 以中序为扩展,其他的改变顺序即可 中序—左中右 栈—右中左1、先入栈根结点2、弹出第一个,因为我们每次先拿到的都是子树的中结点3、右结点入栈 中结点入栈并打上标记null 左结点入栈 4、遇见null结点则弹出两次 第二次的值入列表
class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> list = new ArrayList<>();Deque<TreeNode> de = new LinkedList<>();if(root==null){return list;}else{de.addLast(root);}while(!de.isEmpty()){TreeNode node = de.peekLast();if(node!=null){ de.pollLast();if(node.right!=null){ //右de.addLast(node.right);}de.addLast(node); //中de.addLast(null);if(node.left!=null){ //左de.addLast(node.left);}}else{de.pollLast();node = de.peekLast();de.pollLast();list.add(node.val);}}return list;}
}
二叉数的层序遍历(Leetcode102)
思路1、根结点入队 用int size = de.size() 可以了解到这一层有几个结点2、每层遍历到的结点考虑他的左右结点,如果有结点则入队 先左后右3、这一层遍历到的size个结点加入到arr中 这一层遍历完后再加入到list中
public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> list = new ArrayList<>();Deque<TreeNode> de = new LinkedList<>();if(root==null) {return list;}else {de.add(root);}while(!de.isEmpty()) {int size = de.size();List<Integer> arr = new ArrayList<>();for(int i = 0;i<size;i++) { TreeNode temp = de.pollFirst();if(temp.left!=null) {de.addLast(temp.left);}if(temp.right!=null) {de.addLast(temp.right);}arr.add(temp.val);}list.add(arr);}return list;}
二叉树的层序遍历2(Leetcode107)
思路 先层序遍历 再把顺序颠倒一下即可 Collections.reverse(list); 可以改变集合顺序
public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> list = new ArrayList<>();Deque<TreeNode> de = new LinkedList<>();if(root==null) {return list;}else {de.add(root);}while(!de.isEmpty()) {int size = de.size();List<Integer> arr = new ArrayList<>();while(size-->0) {TreeNode temp = de.pollFirst();if(temp.left!=null) {de.add(temp.left);}if(temp.right!=null) {de.add(temp.right);}arr.add(temp.val);}list.add(arr);}Collections.reverse(list);return list;}
二叉树的右视图(Leetcode199)
思路:每次把每一层最后一个结点的值加入列表即可 也是层序
public List<Integer> rightSideView(TreeNode root) {List<Integer> list = new ArrayList<>();Deque<TreeNode> de = new LinkedList<>();if(root==null) {return list;}else {de.add(root);}while(!de.isEmpty()) {int size = de.size();for(int i = 0;i<size;i++) {TreeNode temp = de.pollFirst();if(i==(size-1)){ //判断是不是当前层最后一个结点list.add(temp.val);}if(temp.left!=null) {de.addLast(temp.left);}if(temp.right!=null) {de.addLast(temp.right);}}}return list;}
二叉树的层平均值(Leetcode637)
class Solution {public List<Double> averageOfLevels(TreeNode root) {List<Double> result = new LinkedList<>();Deque<TreeNode> de = new LinkedList<>();if(root==null) {return result;}de.add(root);while(!de.isEmpty()){int size = de.size();int sum = size;double num = 0;while(size-->0){TreeNode temp = de.pollFirst();num += temp.val;if(temp.left!=null){de.addLast(temp.left);}if(temp.right!=null){de.addLast(temp.right);}}double total = num/sum;result.add(total);}return result;
}
}
N叉树的层序遍历(Leetcode429)
class Solution {public List<List<Integer>> levelOrder(Node root) {List<List<Integer>> result = new LinkedList<>();Deque<Node> de = new LinkedList<>();if(root == null) {return result;}de.add(root);while(!de.isEmpty()) {int size = de.size();List<Integer> list = new LinkedList<>();while(size-->0){Node temp = de.pollFirst();list.add(temp.val);if(temp.children!=null){ //判断是不是有子结点,有的话全部入队for(int i = 0;i<temp.children.size();i++){de.addLast(temp.children.get(i));}}}result.add(list);}return result;}
}
每个树行中找最大值(Leetcode515)
class Solution {public List<Integer> largestValues(TreeNode root) {List<Integer> result = new LinkedList<>();Deque<TreeNode> de = new LinkedList<>();if(root==null) {return result;}de.add(root);while(!de.isEmpty()){int size = de.size();int max = Integer.MIN_VALUE;while(size-->0){TreeNode temp = de.pollFirst();max = max>temp.val?max:temp.val; //判断是不是当前层最大的if(temp.left!=null){de.addLast(temp.left);}if(temp.right!=null){de.addLast(temp.right);}}result.add(max);}return result;}
}
填充每个节点的下一个右侧节点(Leetcode116/117)
思路1、一先创建一个虚拟指针指向root ,用这个指针来操控即可,就不会影响影响到root2、层序遍历3、判断每层的最后一个都指向null 前面的按照队列顺序指向
public Node connect(Node root) {Deque<Node> de = new LinkedList<>();if(root==null) {return root;}else {Node cur = root;de.add(cur);}while(!de.isEmpty()) {int size = de.size();for(int i = 0;i<size;i++) {Node temp = de.pollFirst();if(!de.isEmpty()&&de.peekFirst()!=null&&i!=(size-1)) { //除了最后一个节点外 弹出的指向弹出后的队首temp.next = de.peekFirst();}else {temp.next = null;}if(temp.left!=null) {de.addLast(temp.left);}if(temp.right!=null) {de.addLast(temp.right);}}}return root;}
翻转二叉树(Leetcode226)
思路1、定义一个函数来交换左右节点2、遍历节点,每遍历一个节点交换一次
class Solution {
public TreeNode invertTree(TreeNode root) {Deque<TreeNode> de = new LinkedList<>();if(root==null) {return root;}else {de.add(root);}while(!de.isEmpty()) {int size = de.size();for(int i = 0;i<size;i++) {TreeNode temp = de.pollFirst();if(temp.left!=null) {de.addLast(temp.left);}if(temp.right!=null) {de.addLast(temp.right);}SwapNode(temp);}}return root;}public void SwapNode(TreeNode root) {TreeNode temp = root.left;root.left = root.right;root.right = temp;}
}
对称二叉树(Leetcode101)
思路:1、根节点的左右节点入队2、每次判断左右节点的外侧和内侧节点是否相同3、左右节点的内外侧节点入队
public boolean isSymmetric(TreeNode root) {Deque<TreeNode> de = new LinkedList<>();if(root==null){return true;}else {de.add(root.left);de.add(root.right);}while(!de.isEmpty()) {TreeNode node1 = de.pollFirst();TreeNode node2 = de.pollFirst();if(node1==null&&node2!=null) {return false;}if(node2==null&&node1!=null) {return false;}if(node1==null&&node2==null){continue;}if(node1.val!=node2.val) {return false;}de.add(node1.left);de.add(node2.right);de.add(node1.right);de.add(node2.left);}return true;}