Leetcode中二叉树简单题目题目总结

本文深入探讨了二叉树的各种遍历方法,包括前序、中序、后序和层次遍历,并讲解了如何实现二叉树的镜像翻转、查找最深节点、寻找公共祖先等操作。通过具体代码示例,详细解析了二叉树的递归算法,为读者提供了丰富的实践案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实我最近找了个工作,过的有多废物就先不说了。
这里总结一下二叉树的知识。
二叉树镜像:
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
在这里插入图片描述
做这一题建议先刷完二叉树深度的题目,为啥, 因为深度的题目简单,当然这题也简单是leetcode上简单的题目,不过我有多菜就先不说了。刷完深度的题目,对二叉树递归差不多就有点感觉了。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode MirrorTree(TreeNode root) {
        if(root==null) return null;
        MirrorSingle(root);
       MirrorTree(root.left);
        MirrorTree(root.right);
        return root;
    }
    private void MirrorSingle(TreeNode node)
    {
       TreeNode t = node.left;
       node.left = node.right;
       node.right =t;
    }
}

其实这套框架和二叉树中序遍历,或者某某深度是差不多的,都是先找单个例子,完成单个结点的左右替换,然后在进行左右子树的替换。
然后刷完所有的遍历。我递归做得,对不起我就是这么垃圾。
最难的是层次遍历。
对不起我还是只会递归。
在这里插入图片描述

public class Solution {
        public IList<IList<int>> LevelOrder(TreeNode root)
        {
            IList<IList<int>> res = new List<IList<int>>();
            if (root == null) return res;
            LeverOrder(root, res, 0);
            return res;
        }
        private void LeverOrder(TreeNode root,IList<IList<int>> res,int depth)
        {
            if (root == null) return;
            if(res.Count<=depth)
            {
                res.Add(new List<int>());
            }
            res[depth].Add(root.val);
            LeverOrder(root.left, res, depth + 1);
            LeverOrder(root.right, res, depth + 1);
        }
}

在这里插入图片描述
中序遍历,是顺序的依次来求解。

public class Solution {
        int count = 0;
        int curVal = -100;
        int max = -1;
        public int[] FindMode(TreeNode root)
        {
            List<int> lst = new List<int>();
            dfs(root, lst);
            return lst.ToArray();
        }
        private void dfs(TreeNode root,List<int> lst)
        {
            if (root == null) return;
            if(root.left!=null)
            dfs(root.left,lst);
           if(root.val!=curVal)
            {
                count = 1;
                curVal = root.val;
               if(count>max)
                {
                    if(lst.Contains(root.val)==false)
                    {
                        lst.Clear();
                        lst.Add(root.val);
                    }
         
                    max = count;
                }
                else if(count==max)
                {
                    if(lst.Contains(root.val)==false)
                        lst.Add(root.val);
                }
            }
            else
            {
                ++count;
                if(count>max)
                {
                      lst.Clear();
                        lst.Add(root.val);
         
                    max = count;
                }
                else if(count==max)
                {
                    if(lst.Contains(root.val)==false)
                        lst.Add(root.val);
                }
            }
            if(root.right!=null)
                dfs(root.right,lst);
        }
}

在这里插入图片描述

public class Solution {
        public bool IsSymmetric(TreeNode root)
        {
            if(root==null) return true;
            return isSymmetric(root.left,root.right);
        }
        private bool isSymmetric(TreeNode r1,TreeNode r2)
        {
            if(r1!=null&&r2!=null)
            {
                return 
                        r1.val==r2.val
                        &&
                        isSymmetric(r1.left,r2.right)
                        &&
                        isSymmetric(r2.left,r1.right);
            }
            else if(r1==null&&r2==null) return true;
            else return false;
        }
}

在这里插入图片描述
一种比较经典的DFS搜索算法,大概就是传个值进去找到直接赋值返回就行了。

    public TreeNode SearchBST(TreeNode root, int val) {
        if(root==null) return null;
        TreeNode dummy=null;
        dfs(root,val,ref dummy);
        return dummy;
    }
    private void dfs(TreeNode root,int val,ref TreeNode dummy)
    {
        if(root==null) return;
        if(dummy==null&&root.val==val){
            dummy = root;
            return;
        }
        if(dummy==null)
        {
          dfs(root.left,val,ref dummy);
          dfs(root.right,val,ref dummy);
        }
    }

在这里插入图片描述
居然也能也能有100%的题目。
在这里插入图片描述

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null) return null;
        if(p==null||q==null) return null;
        if(p.val<root.val&&q.val>root.val) 
        {
            return root;
        }
        else if(p.val>root.val&&q.val<root.val)
        {
            return root;
        }
        else if(p==root)
        {
            return p;
        }
        else if(q==root)
        {
            return q;
        }
        else if(p.val<root.val&&q.val<root.val)
        {
            return lowestCommonAncestor(root.left,p,q);
        }
        else if(p.val>root.val&&q.val>root.val)
        {
            return lowestCommonAncestor(root.right,p,q);
        }
        return null;
    }

记录一种工作中用的dfs。

public Transform FindChildNode(Transform parent,string nodeName){
	//先序遍历
	var node = parent.Find(nodeName);
	if(node!=null) return node;
	//遍历parent的每一个孩子
	foreach(var tran in parent){
		node = FindChildNode(tran,nodeName);
		if(node!=null)
		{
			return node;
		}
	}
	return null;
}

做到这里反正对二叉树的遍历就是有点感觉了,就是把leetcode上关于树的简单的题目都刷一遍,我刷到最后就是佛系了,对二叉树的遍历有点麻木了。就比如说这一题,虽然我做的也贼几把low,但是却是很无脑就能做出来了,没有那种生硬的感觉。
在这里插入图片描述
在这里插入图片描述
5分钟就搞定了,说明有点感觉了。

public class Solution {
    public bool IsCousins(TreeNode root, int x, int y) {
        if(root==null) return false;
        if(root.val==x||root.val==y) return false;
        int dxl = search(root.left,x);
        int dxr = search(root.right,x);
        int dyl = search(root.left,y);
        int dyr = search(root.right,y);
       if(dxl!=-1&&dxl==dyr)
       {
           return true;
       }
       if(dxr!=-1&&dxr==dyl)
       {
           return true;
       }
       return IsCousins(root.left,x,y) || IsCousins(root.right,x,y);
    }
    private int search(TreeNode parent,int p)
    {
        if(parent==null) return -1;
        int dl = dfs(parent.left,p,0);
        if(dl!=-1)
        {
            return dl;
        }
        int dr = dfs(parent.right,p,0);
        if(dr!=-1)
        {
            return dr;
        }
        return -1;
    }
    private int dfs(TreeNode root,int p,int depth)
    {
        if(root==null) return -1;
        if(root.val==p)
        {
            return depth;
        }
        int dl = dfs(root.left,p,depth+1);
        if(dl!=-1)
        {
            return dl;
        }
        int dr = dfs(root.right,p,depth+1);
        if(dr!=-1)
        {
            return dr;
        }
        return -1;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JustEasyCode

谢谢您

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值