9.26lc
开头:今天学习二叉树的基本玩法:
重点: Mrrios 遍历:
个人感受: 传统的二叉树的递归遍历 都是 一个节点 循环 3次输出:根据先中后 选择来:且空间 复杂度 (n)
:但是 Mrrios 遍历的遍历 只用了几个指针 没使用额外的 辅助空间: Mrrios 遍历 感觉 像是人为的去控制了 遍历的次数 从而达到了 空间资源的优化~ 很巧妙:
:核心代码:
public static morris(Node head){
if(head == null){
return null;
}
Node cur=head;
Node mostRight =null;
while(cur != null){
//cur != null 就可以继续
//mostRight == cur的左子树
mostRight=cur.left;
if(mostRight !=null){
//找到 左子树的最有子节点
while(mostRight.right != null && mostRight.right ==cur){
mostRight=mostRight.right;
}
//第一次来到这个节点
if(mostRight.right ==null){
mostRight.right =cur;
cur=cur.left;
continue;
}else{
//相当于 mostRight =cur 是二次来到这里: 值为null 同事 左子树遍历完 往右走
mostRight.right =null;
cur=cur.right;
continue;
System.out.println("da");
}
}
// 如果mostRight 为空 直接 往右移动
cur=cur.right;
}
时间复杂度 n
}
144. 二叉树的前序遍历 - 力扣(LeetCode) (leetcode-cn.com)
:递归 要不就是 迭代~~
学了一个 Morris遍历~~
/**
* 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> list=new ArrayList<>();
if(root == null){
return list ;
}
Deque<TreeNode> stack = new LinkedList<TreeNode>();
stack.push(root);
while(! stack.isEmpty()){
TreeNode cur =stack.poll();
list.add(cur.val);
if(cur.right != null) {
stack.push(cur.right);
}
if(cur.left != null){
stack.push(cur.left);
}
}
return list;
}
}
145. 二叉树的后序遍历 - 力扣(LeetCode) (leetcode-cn.com)
/**
* 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> list=new LinkedList<>();
p(root,list);
return list;
}
public static void p(TreeNode head,List<Integer> list){
if(head == null) return ;
p(head.left,list);
p(head.right,list);
list.add(head.val);
}
}
94. 二叉树的中序遍历 - 力扣(LeetCode) (leetcode-cn.com)
/**
* 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> list =new LinkedList<>();
TreeNode mostRight=null;
while(root != null){
mostRight=root.left;
if(mostRight !=null){
while(mostRight.right !=null && mostRight.right !=root){
mostRight =mostRight.right;
}
if(mostRight.right == null){
mostRight.right=root;
root=root.left;
continue;
}else{
list.add(root.val);
mostRight =null;
root =root.right;
}
}else{
list.add(root.val);
root=root.right;
}
}
return list;
}
}
102. 二叉树的层序遍历 - 力扣(LeetCode) (leetcode-cn.com)
:这是一个 用 bfs 好于dfs的:
一共三个辅助:
- 整体结果集 res
- 每层结果集 level
- 管理队列 queue
/**
* 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<List<Integer>>();
int size=0;
//遍历管理的的 queue
Queue<TreeNode> queue =new LinkedList<TreeNode>();
//存储结果的
if(root == null) return res;
queue.add(root);
while(! queue.isEmpty()){
List<Integer> reslut =new LinkedList<>();
// queue.size() 来得到了 每层的宽度~
size =queue.size();
for(int i=1;i<=size;i++){
TreeNode cur =queue.poll();
reslut.add(cur.val);
if(cur.left !=null){
queue.add(cur.left);
}
if(cur.right !=null){
queue.add(cur.right);
}
}
res.add(reslut);
}
return res;
}
}
101. 对称二叉树 - 力扣(LeetCode) (leetcode-cn.com)
:这道题 上来就想错了~~~~
: 我的思路: 整体的中序遍历是个 对称数组 就是个 堆成二叉树 实际有反例~~
:正确的 直接递归:
:总结递归的思路:
- 题目 需要单一的信息 例如 这道题:
- 单一信息的所需条件: 关于二叉树的 基本就是 左子树 右子树 所附带的信息~~
- 递归的结束条件
/**
* 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 boolean isSymmetric(TreeNode root) {
return p(root,root);
}
public boolean p(TreeNode i,TreeNode j){
if( i == null && j ==null){
return true;
}
if(i ==null || j == null){
return false;
}
return i.val ==j.val &&p(i.left,j.right) && p(i.right,j.left);
}
}
104. 二叉树的最大深度 - 力扣(LeetCode) (leetcode-cn.com)
:递归
/**
* 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 int maxDepth(TreeNode root) {
if(root == null) return 0;
return p(root);
}
public int p(TreeNode root){
if(root.left == null && root.right == null) return 1;
//int 值
int lefth=Integer.MIN_VALUE;
int righth=Integer.MIN_VALUE;
if(root.left !=null){
lefth=p(root.left);
}
if(root.right !=null){
righth=p(root.right);
}
return 1+Math.max(lefth,righth);
}
}
111. 二叉树的最小深度 - 力扣(LeetCode) (leetcode-cn.com)
/**
* 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 int minDepth(TreeNode root) {
if(root == null) return 0;
return p(root);
}
public int p(TreeNode cur){
if(cur.left == null && cur.right == null) return 1;
int l =Integer.MAX_VALUE;
int r =Integer.MAX_VALUE;
if(cur.left != null){
l=p(cur.left);
}
if(cur.right != null){
//错误!!! 写成 l
r=p(cur.right);
}
return (1+Math.min(l,r));
}
}