首页 > 编程语言 >代码随想录算法训练营第十四天(统一迭代;LeetCode226.翻转二叉树;LeetCode101.对称二叉树;LeetCode104.二叉树的最大深度;LeetCode111.二叉树的最小深度)

代码随想录算法训练营第十四天(统一迭代;LeetCode226.翻转二叉树;LeetCode101.对称二叉树;LeetCode104.二叉树的最大深度;LeetCode111.二叉树的最小深度)

时间:2024-11-27 20:29:22浏览次数:9  
标签:node right TreeNode val 第十四天 二叉树 深度 root left

统一迭代

LeetCode 144. 二叉树的前序遍历

题目链接:二叉树的前序遍历题目链接

代码

/**
 * 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> result=new LinkedList<>();
        Stack<TreeNode> stack=new Stack<>();
        if(root!=null) stack.push(root);
        while(!stack.isEmpty())
        {
            TreeNode node=stack.peek();
            if(node!=null)
            {
                stack.pop();
                if(node.right!=null) stack.push(node.right);
                if(node.left!=null) stack.push(node.left);
                stack.push(node);
                stack.push(null);
            }
            else
            {
                stack.pop();
                node=stack.peek();
                stack.pop();
                result.add(node.val);
            }
        }
        return result;
    }
}

LeetCode 94. 二叉树的中序遍历

题目链接:二叉树的中序遍历

代码

/**
 * 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>result=new LinkedList<>();
        Stack<TreeNode>stack=new Stack<>();
        if(root!=null)  stack.push(root);
        while(!stack.isEmpty())
        {
            TreeNode node=stack.peek();
            if(node!=null)
            {
                stack.pop();
                if(node.right!=null) stack.push(node.right);
                stack.push(node);
                stack.push(null);
                if(node.left!=null) stack.push(node.left);
            }
            else
            {
                stack.pop();
                node=stack.peek();
                stack.pop();
                result.add(node.val);
            }
        }
        return result;
    }
}

LeetCode 145. 二叉树的后序遍历

题目链接:二叉树的后序遍历题目链接

代码

/**
 * 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> result=new LinkedList<>();
        Stack<TreeNode> stack=new Stack<>();
        if(root!=null)  
            stack.push(root);
        while(!stack.isEmpty())
        {
            TreeNode node=stack.peek();
            if(node!=null)
            {
                stack.pop();
                stack.push(node);
                stack.push(null);
                if(node.right!=null) stack.push(node.right);
                if(node.left!=null) stack.push(node.left);
            }
            else
            {
                stack.pop();
                node=stack.peek();
                stack.pop();
                result.add(node.val);
            }
        }
        return result;
    }
}

LeetCode 226. 翻转二叉树

题目链接:翻转二叉树题目链接

思路

该题目的关键就在于交换一个节点的左右孩子,所以使用前序遍历和后序遍历交换应该都可以,使用中序遍历进行交换是不可以的,因为使用中序遍历进行交换会导致某些节点交换两次。

代码

/**
 * 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 TreeNode invertTree(TreeNode root) {
        //前序遍历递归方法
        if(root==null)
            return root;
        reverse(root);
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
    public void reverse(TreeNode root)
    {
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }
}
/**
 * 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 TreeNode invertTree(TreeNode root) {
        //统一迭代法(前序遍历)
        Stack<TreeNode>stack=new Stack<>();
        if(root!=null) stack.push(root);
        while(!stack.isEmpty())
        {
            TreeNode node=stack.peek();
            if(node!=null)
            {
                stack.pop();
                if(node.right!=null) stack.push(node.right);
                if(node.left!=null) stack.push(node.left);
                stack.push(node);
                stack.push(null);
            }
            else
            {
                stack.pop();
                node=stack.peek();
                stack.pop();
                reverse(node);
            }
        }
        return root;
    }
    public void reverse(TreeNode root)
    {
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }
}
/**
 * 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 TreeNode invertTree(TreeNode root) {
        //迭代法(前序遍历)
        Stack<TreeNode>stack=new Stack<>();
        if(root!=null)  stack.push(root);
        while(!stack.isEmpty())
        {
            TreeNode node=stack.peek();
            stack.pop();
            reverse(node);
            if(node.right!=null) stack.push(node.right);
            if(node.left!=null) stack.push(node.left);
        }
        return root;
    }
    public void reverse(TreeNode root)
    {
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
    }
}
/**
 * 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 TreeNode invertTree(TreeNode root) {
    	//层序遍历
        ArrayDeque<TreeNode> deque=new ArrayDeque<>();
        if(root!=null)
            deque.offer(root);
        while(!deque.isEmpty())
        {
            int size=deque.size();
            while(size-->0)
            {
                TreeNode node=deque.poll();
                reverse(node);
                if(node.right!=null)
                    deque.offer(node.right);
                if(node.left!=null)
                    deque.offer(node.left);
            }
        }
        return root;
    }
    public void reverse(TreeNode node)
    {
        TreeNode temp=node.left;
        node.left=node.right;
        node.right=temp;
    }
}

LeetCode 101. 对称二叉树

题目链接:对称二叉树题目链接

代码

/**
 * 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) {
        if(root==null)
            return true;
        return compare(root.left,root.right);
    }
    public boolean compare(TreeNode left,TreeNode right)
    {
    	//递归法
        if(left!=null&&right==null)
            return false;
        if(left==null&&right!=null)
            return false;
        if(left==null&&right==null)
            return true;
        if(left.val!=right.val)
            return false;
        boolean compareInside=compare(left.right,right.left);
        boolean compareOutside=compare(left.left,right.right);
        return compareInside&&compareOutside;
    }
}
/**
 * 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) {
        //迭代法
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root.left);
        queue.offer(root.right);
        while(!queue.isEmpty())
        {
            TreeNode left=queue.poll();
            TreeNode right=queue.poll();
            if(left==null&&right==null)
                continue;
            if(left!=null&&right==null)
                return false;
            if(left==null&&right!=null)
                return false;
            if(left.val!=right.val)
                return false;
            queue.offer(left.left);
            queue.offer(right.right);
            queue.offer(left.right);
            queue.offer(right.left);
        }
        return true;
    }
}

LeetCode 104. 二叉树的最大深度

题目链接:二叉树的最大深度题目链接

思路

该题目可以使用递归法、回溯法、层序遍历。

代码

/**
 * 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;
        int leftdepth=maxDepth(root.left);
        int rightdepth=maxDepth(root.right);
        int depth=Math.max(leftdepth,rightdepth)+1;
        return depth;
    }
}
/**
 * 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 {
    int result;
    public int maxDepth(TreeNode root) {
        result=0;
        if(root==null)
            return result;
        getDepth(root,1);
        return result;
        
    }
    public void getDepth(TreeNode node,int depth)
    {
        //回溯+前序遍历
        if(depth>result)
            result=depth;
        if(node.left==null&&node.right==null)
            return;
        if(node.left!=null)
        {
            depth++;
            getDepth(node.left,depth);
            depth--;
        }
        if(node.right!=null)
        {
            depth++;
            getDepth(node.right,depth);
            depth--;
        }
        return;
    }
}

LeetCode 111. 二叉树的最小深度

题目链接:二叉树的最小深度题目链接

思路

该题目可以使用递归法和层序遍历

代码

/**
 * 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;
        int left=minDepth(root.left);
        int right=minDepth(root.right);
        if(root.left!=null&&root.right==null)
            return 1+left;
        if(root.left==null&&root.right!=null)
            return 1+right;
        return 1+Math.min(left,right);
    }
}
/**
 * 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;
        Deque<TreeNode>deque=new LinkedList<>();
        deque.offer(root);
        int depth=0;
        while(!deque.isEmpty())
        {
            int size=deque.size();
            depth++;
            for(int i=0;i<size;i++)
            {
                TreeNode node=deque.poll();
                if(node.left==null&&node.right==null)
                    return depth;
                if(node.left!=null)
                    deque.offer(node.left);
                if(node.right!=null)
                    deque.offer(node.right);
            }
        }
        return depth;
    }
}

标签:node,right,TreeNode,val,第十四天,二叉树,深度,root,left
From: https://blog.csdn.net/qq_51597940/article/details/144093707

相关文章

  • 构建与计算:使用递归实现表达式的二叉树解析器
    ✅作者简介:2022年博客新星第八。热爱国学的Java后端开发者,修心和技术同步精进。......
  • 深度学习笔记——常见的Transformer位置编码
    本文详细介绍3种常见的Transformer位置编码——正弦/余弦位置编码(sin/cos)、基于频率的二维位置编码(2DFrequencyEmbeddings)、旋转式位置编码(RoPE)文章目录Transformer中常见的编码方式正弦/余弦位置编码(SinusoidalPositionalEncoding)基于频率的二维位置编码(2DFr......
  • Java并发工具类深度解析
    目录1.ConcurrentHashMap1.1原理1.2示例2.AtomicInteger2.1原理2.2CAS操作图解2.3代码示例3.Semaphore3.1原理3.2Semaphore工作流程3.3代码示例4.CyclicBarrier4.1原理4.2CyclicBarrier工作流程4.3代码示例5.CountDownLatch5.1原理5.2CountDownLat......
  • 360 度评估大揭秘:团队报告深度解析
    在360度评估中,团队报告是至关重要的一环。其扉页清晰展示了评估项目名称及“团队报告”字样,例如“360°评估问卷【参考示例】-团队报告”,同时明确团队人数和报告日期。 前言部分阐明,此报告通过汇总被评价人得分,从评价关系、指标或行为的得分进行对比分析,旨在了解团队能力......
  • 抖音短视频矩阵怎么做?源头矩阵厂家深度解析矩阵玩法
    在2024年激烈的市场竞争中,企业要想实现业绩的飞跃,就必须突破流量的瓶颈。短视频作为新兴的流量聚集地,企业需要迅速把握机遇,打造一个强大的短视频网络。谁能抢先一步行动,谁就能在这场流量争夺战中取得先机。今天,笔者一文给大家聊清楚,什么是短视频矩阵?实体行业为什么要做短视......
  • [Python手撕]二叉树的锯齿形层序遍历
    二叉树的锯齿形层序遍历给你二叉树的根节点root,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。示例1:输入:root=[3,9,20,null,null,15,7]输出:[[3],[20,9],[15,7]]示例2:输入:root=[1]输出:[[1]]示例3:输入:root......
  • 【大数据分析&深度学习】在Hadoop上实现分布式深度学习
    【作者主页】FrancekChen【专栏介绍】⌈⌈⌈智能大数据分析⌋......
  • 使用Python实现智能食品安全追溯系统的深度学习模型
    食品安全一直是社会关注的重大问题,尤其在全球化供应链日益复杂的今天,食品安全追溯系统显得尤为重要。通过智能食品安全追溯系统,可以有效追溯食品来源、流通路径,及时发现和处理食品安全问题。本文将详细介绍如何使用Python构建一个智能食品安全追溯系统的深度学习模型,并通过......
  • 使用Python实现智能食品供应链优化的深度学习模型
    在现代食品工业中,供应链的优化对于保证食品质量、降低成本和减少浪费至关重要。通过深度学习技术,可以实现智能化的供应链优化,有效提升供应链的效率。本文将详细介绍如何使用Python构建一个智能食品供应链优化的深度学习模型,并通过具体代码示例展示实现过程。项目概述本项......
  • 226. 翻转二叉树
    问题描述给你一棵二叉树的根节点root,翻转这棵二叉树,并返回其根节点。法一、递归单独递归函数,返回TreeNode*x,x是从当前层开始的逆转后的树的根结点classSolution{public:TreeNode*solve(TreeNode*root){if(root==nullptr){returnnull......